Initializer and declaration error

So the error comes up at "void read()" it says "expected initializer before ‘void’" im quite new to cpp and need help on this one. thanks beforehand
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>

using namespace std;

#define ARR_LEN 5

int arr[ARR_LEN]

void read(){
     cout<<"enter "<<ARR_LEN<<" numbers"<<endl;
      for(int j=0; j<ARR_LEN; j++){
        cin>>arr[j];
    }
}
void write(){
     cout<<"array is:"<<endl;
     for(int i=0; i<ARR_LEN; i++){
         cout<<arr[i]<<" ";
     }
    cout<<endl;
}
int main(){
    read();
    int iMax = 0;
    for(int i=0; i<ARR_LEN; i++){
        if(arr[i]>arr[iMax]){
            max=arr[i];
            iMax=i;
        }
    }
    cout<<"max index: "<<iMax<<endl;

    write();
    int temp = arr[iMax];
    arr[iMax]= arr[0];
    arr[0] = temp;

    write();
    return 0;
}
Add a semicolon at the end of line 7.

Remove line 27.
int arr[ARR_LEN];
Your code looks very c-ish.
Consider using modern C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <array>
#include <algorithm>
#include <utility>
using namespace std;

using Array = std::array<int, 5>;

void read(Array& arr)
{
    cout<<"enter "<< arr.size() <<" numbers"<<endl;
    for(auto& num : arr)
    {
        cin>> num;
    }
}
void write(Array& arr)
{
    cout<<"array is:"<<endl;
    for(auto num: arr )
    {
        cout<< num <<" ";
    }
    cout<<endl;
}
int main()
{
    Array arr;

    read(arr);

    auto max_val = std::max_element(arr.begin(), arr.end());
    int max_idx = std::distance(arr.begin(), max_val) ;
    cout << "max index: "<< max_idx << '\n';
    cout << "max_val: " << *max_val << '\n';

    write(arr);
    std::swap(arr[0], arr[max_idx]);

    write(arr);
    return 0;
}

Output:

enter 5 numbers
1 2 3 4 5
max index: 4
max_val: 5
array is:
1 2 3 4 5
array is:
5 2 3 4 1
Thank you. you people are amazing! i appreciate it.
Topic archived. No new replies allowed.