Arrays

Write a three function program (your main function and two other functions).
The main function of the program should create an array that can store 10 integers.
The main function will then pass the array to a second function that gets the integers from the user and stores them in the array.
Then your main will call a third function that displays the elements in the array in the reverse order.
You must not use global variables.
Pass the array and the number of elements in the array as arguments to both of the called functions.

Here is some sample output:
Enter number 1: 12[Enter]
Enter number 2: 15[Enter]
Enter number 3: 4[Enter]
Enter number 4: 65[Enter]
Enter number 5: 42[Enter]
Enter number 6: 19[Enter]
Enter number 7: 5[Enter]
Enter number 8: 7[Enter]
Enter number 9: 33[Enter]
Enter number 10: 87[Enter]

The values in reverse order are:
Value number 10 is 87
Value number 9 is 33
Value number 8 is 7
Value number 7 is 5
Value number 6 is 19
Value number 5 is 42
Value number 4 is 65
Value number 3 is 4
Value number 2 is 15
Value number 1 is 12


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
#include <iostream>

using namespace std;

int main()
{
   int arr[10];
   int n = 10;
   readElements(arr, n);
   printElements(arr, n);
   
   return 0;
}

void readElements(int arr[], int n)
{
   int i = 0;
   
   for(i = 0; i < n; i++)
   {
      cout << "Enter number" + i + ":";
      cin >> arr[i];
      cout << endl;
   }
   
} 

void printElements(int arr[], int n)
{
   int i = 0;
   
   for(i = 0; i < n; i++)
   {
      cout << "Value number" + i + "is";
      cout << arr[i];
      cout << endl;
   }
   
}


I need help with my code, I keep getting errors and it doesn't print the output.
closed account (Dy7SLyTq)
i bet your error is something about conversion between char *'s (ie string literals) and int. don't do "Value number" + i + "is". do "Value Number"<< i <<"is"
Would be nice if you included what errors it gives in future.

The string literals will cause problems as mentioned above. Also seeing as C++ reads from top to bottom it has no idea that "readElements" and "printElements" exist yet when you call them from main. You will either want to move both of those functions above main or use forward declarations.
http://www.learncpp.com/cpp-tutorial/17-forward-declarations/

For your printElements it says to print them in reverse (so your for loop wont be starting at 0 in that case)

Sorry! Here are the errors that I get

In function 'int main()':
14: error: 'readElements' was not declared in this scope
15: error: 'printElements' was not declared in this scope
In function 'void readElements(int*, int)':
26: error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'
In function 'void printElements(int*, int)':
39: error: invalid operands of types 'const char*' and 'const char [3]' to binary 'operator+'
Those should all be fixed with the suggestions above.
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
#include <iostream>

using namespace std;

void readElements(int arr[], int n)
{
   int i = 1;
   
   for(i = 1; i < n; i++)
   {
      cout << "Enter number " << i << ": ";
      cin >> arr[i];
   }
   
} 

void printElements(int arr[], int n)
{
   int i = 1;
   
   for(i = 1; i < n; i++)
   {
      cout << "Value number " << i << " is ";
      cout << arr[i];
      cout << endl;
   }
   
}              

int main()
{
   int arr[10];
   int n = 10;
   readElements(arr, n);
   printElements(arr, n);
   
   return 0;
}

Enter number 1: 12
Enter number 2: 15
Enter number 3: 4
Enter number 4: 65
Enter number 5: 42
Enter number 6: 19
Enter number 7: 5
Enter number 8: 7
Enter number 9: 33
Value number 1 is 12
Value number 2 is 15
Value number 3 is 4
Value number 4 is 65
Value number 5 is 42
Value number 6 is 19
Value number 7 is 5
Value number 8 is 7
Value number 9 is 33


Here is my code and output, I still need to enter a 10th number and the value numbers should be printed in reverse.
I got my code to print out 10 numbers by changing i < n to i <= n
but I still need help printing the values out in reverse.

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>

using namespace std;

void readElements(int*, int);
void printElements(int*, int);

int main()
{
   int arr[10];
   int n = 10;
   readElements(arr, n);
   printElements(arr, n);

   return 0;
}

void readElements(int arr[], int n)
{
   int i = 0;

   for(i = 0; i < n; i++)
   {
      cout << "Enter number " << i+1 << ": ";
      cin >> arr[i];
     // cout << endl;
   }

}

void printElements(int arr[], int n)
{
   int i = 0;
   cout << "\nThe values in reverse order are:\n";
   for(i = n-1; i >= 0; i--)
   {
      cout << "Value number " << i+1 << " is ";
      cout << arr[i];
      cout << endl;
   }

}
Topic archived. No new replies allowed.