• Нормализовать элементы вектора (найти максимум и разделить все элементы на максимум). Элементы вектора вводятся с клавиатуры и хранятся в связанном списке.

    Напишите пожалуйста код C++ с помощью связного списка. Я умею только с помощью векторов, вот мой код через векторы на всякий случай:

    #include
    #include
    #include
    using namespace std;
    void main()
    {setlocale(0,"Rus");
    vector myVector(10);
    for(int i = 0; i < myVector.size(); i++)
    cin >> myVector[i];
    double max=-1;
    for(int i = 0; i < myVector.size(); i++)
    if (myVector[i]>max)
    max=myVector[i];
    cout< for(int i = 0; i < myVector.size(); i++)
    cout<< myVector[i]<< " ";
    cout< cout< for(int i = 0; i < myVector.size(); i++)
    { myVector[i]=myVector[i]/max;
    cout<< myVector[i]<< " ";}
    cout< system("pause");
    }
    (библиотеки не отобразились почему-то, там иострим, вектор и стд.афикс помоему)

Ответы 2

  • Спасибо огромное, очень помогли!
    • Автор:

      montoro
    • 5 лет назад
    • 0
  • #include <iostream>using namespace std;struct link {     double data;     link* next; };  link* first = NULL;  void additem(int d) // добавление элементов список{     link* newlink = new link;     newlink->data = d;     newlink->next = first;     first = newlink; }  void display() // вывод на экран{     link* current = first;     while(current)     {         cout << current->data << " ";         current = current->next;     }  }  int max() // max{     link* current = first;     int m=0;    while(current)     {         if (current->data > m) m = current->data;         current = current->next;     }    return m;} void norm(int m) // нормализация{     link* current = first;     while(current)     {         current->data /= m;         current = current->next;     }  } int main() {     int n=10;    int a;    for (int i=1; i<n; i++) {    cin >> a;    additem(a);     }    display();    cout << endl << max() << endl;    norm(max());    display();}Пример:64 45 19 42 83 38 71 23 52 83 0.771084 0.542169 0.228916 0.506024 1 0.457831 0.855422 0.277108 0.626506
  • Добавить свой ответ

Войти через Google

или

Забыли пароль?

У меня нет аккаунта, я хочу Зарегистрироваться

How much to ban the user?
1 hour 1 day 100 years