Help with a task /C++/

I'm really worried because I need this done until Monday.

This is the task:
Create a program with function main() and menu with functions for:
A ) Inserting several numbers from the keyboard into one-dimensional array (the numbers should be 5);
B ) Overwriting of the 1st array into second array in which the elements are put in reverse order.
C ) Overwriting of the second array into third array in which the elements are put in descending order.
D)Displaying information about the array. (I guess this is A,B and C)

The problem is that when I type the numbers I get some random number for an array...The other problem is that after I've written the numbers I also can't get back to 2,3, or 4...What should I add to my existing code?


Here is the code:
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;
int main ()
{
char choice;
cout << "1. Insert 5 numbers" << endl;
cout << "2. Reverse order of the numbers" << endl;
cout << "3. Ascending order"<< endl;
cout << "4. Display 1, 2 i 3"<<endl;
cout << "\n Your choice is:" <<endl;
	cin >> choice;
	switch (choice)
		{
	case '1': 
	int array[5];
	cout << "Write the numbers here:" << endl;
        int i;
        for (i=0; i<5;i++)
        cin >> array[i];
        cout << "Array : " << array[i] << endl; break;
			
    case '2':
	    int reverseorder[5];
	    for (int i=4; i>=0 ;i--)
    	cout << reverseorder [i]; break;
			
	case '3':				
	    int ascendingorder[5];
	    for (i=0; i<5; i++);
	    cout << ascendingorder [i];	break;
			
	case '4':
	     cout << "Array"<< array[i] << endl;
         cout << "Reverse order" << reverseorder[i] << endl;
         cout << "Ascending order" << ascendingorder[i] << endl;break;
	
		default: cout << "Wrong data" << endl ; break;	
			
		};
				
	system ("pause");
		}
Firstly, if you want an array that holds five numbers, you must declare it like this: int array[4]; not with [5], because that way you create an array that holds 6 numbers. Also try to hold the cases of the switch loop in brackets.

At line 33, you have the variable i unitialized, because you are inside a new case of the switch loop. Fix these things and see how it works
Minuss273 you're talking nonsense. If you want an array with 5 elements then you use [5].

OP: here's an example of how you can do one and two. for three, have a google of "bubble sort".

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

int main()
{
	const int ARRSIZE = 5;

	int array[ARRSIZE];
	std::cout << "Write the numbers here (enter ater each one): " << std::endl;
	
	for (int i = 0; i < ARRSIZE; i++)
	{
		std::cin >> array[i];
	}


	// print out, same as above
	for (int i = 0; i < ARRSIZE; i++)
	{
		std::cout << "element[" << i+1 << "] = " << array[i] << std::endl;
	}

	int reverseArray[ARRSIZE];

	for (int i = 0; i < ARRSIZE; i++)
	{
		reverseArray[ARRSIZE - 1 - i] = array[i];
	}

	return 0;
}


and for four, it's just lines 10 to 13, with whichever array you need to display.
Last edited on
mutexe correct me if i am wrong, but doesnt every array start at position 0, so if i want five elements, i would have 0, 1, 2, 3, 4, as positions. So there would be stored five elements at these positions?
Last edited on
mutexe correct me if i am wrong, but doesnt every array start at position 0, so if i want five elements, i would have 0, 1, 2, 3, 4, as positions. So there would be stored five elements at these positions?


That's correct, however, mutexe was responding to your post which is incorrect:
Firstly, if you want an array that holds five numbers, you must declare it like this: int array[4]; not with [5],


There is a difference between declaring an array and referencing an array. When you declare an array, your specifiy the number of elements you want the array to hold, so if you want it to hold five elements, you declare it as int array[5];

When referencing the elements, you reference the five elements as [0] through [4].

Declaring int array[4]; would only generate storage for 4 elements.

Alright AbstractionAnon, now i can see clearly, thanks! :)
Minuss273 you're talking nonsense.

I shouldn't have phrased it like that, my apologies. but it sounds like you understand now, which is cool :)
It's ok :)
Topic archived. No new replies allowed.