i have an array of "2,3,4,5,6,9,10,11,12,99".
i need to create a table of this which i have done using case
1. Find the maximum value of the array
2. Find the sum of the first and last element of the array
3. Swap the adjacent pairs of arrays
4. Display the values in array
5. Quit
Please enter choice:
but when i try running the program i did i keep having the break or continue pop up anyone can tell me what did i do wrong?
#include <iostream>
usingnamespace std;
int main()
{
int choice;
int number[10] = {2,3,4,5,6,9,10,11,12,99};
int x;
while(true)
{
cout << "1. Find the maximum value of the array" << endl;
cout << "2. Find the sum of the first and last element of the array" << endl;
cout << "3. Swap the adjacent pairs of arrays" << endl;
cout << "4. Display the values in array" << endl;
cout << "5. Quit" << endl;
cout << "Please enter choice:" << endl;
cin >> choice;
if (choice == 5)
break; //get out of while loop
else
{
switch(choice)
{
case 1 : cout <<"Option 1 selected : " << endl;
cout <<number[9] << endl;
break; //get out of switch block
case 2 : cout <<"Option 2 selected : " << endl;
cout << number[0] + number[9] << endl; //adding array 1 and 10
break; //get out of switch block
case 3 : cout <<"Option 3 selected : " << endl;
x = number[0];
number[0] = number[1];
number[1] = x; //swap 1 and 2
x = number[2];
number[2] = number[3];
number[3] = x; //swap 3 and 4
x = number[4];
number[4] = number[5];
number[5] = x; //swap 5 and 6
x = number[6];
number[6] = number[7];
number[7] = x; //swap 7 and 8
x = number[8];
number[8] = number[9];
number[9] = x;//swap 9 and 10
cout << number <<endl; //show new array
break; //get out of switch block
case 4 : cout <<"Option 4 selected : " << number << endl; //show the numbers;
break; //get out of switch block
default : cout <<"Invalid choice" << endl;
}
}
}
cout << "End of programme" << endl;
}
One thing I see - arrays start numbering at 0, not 1, so the valid elements for an array of size 10 will be 0 through 9, not 1 through 10.
Edit- if you can edit your post, highlight the code and then click the <> button on the right side in the Format table, that'll format your code and make it easier to read :)
Ok i have change all the [] but when i run the program again the same thing will happen when i press 3 to swap the arrays, it will show some numbers and alphabet like "0021FE58"
same thing goes for case 4
The name of the array is sort of a pointer to the first element of the array. You're printing out a memory address if you just try to cout << the name of the array.
To print out the values of an array, you can use a for loop.
1 2
for (int i = 0; i < 10; ++i)
cout << number[i] << " ";
Nice i got it but how to i put it into a line format than
The values are : 3
The values are : 2
The values are : 5
The values are : 4
.
.
.
.
.
The values are : 12
#include <iostream>
usingnamespace std;
int main()
{
int temp = 0;
int choice;
int number[10] = {2,3,4,5,6,9,10,11,12,99};
int x;
while(true)
{
cout << "1. Find the maximum value of the array" << endl;
cout << "2. Find the sum of the first and last element of the array" << endl;
cout << "3. Swap the adjacent pairs of arrays" << endl;
cout << "4. Display the values in array" << endl;
cout << "5. Quit" << endl;
cout << "Please enter choice:" << endl;
cin >> choice;
if (choice == 5)
break; //get out of while loop
else
{
switch(choice)
{
case 1 : cout <<"Option 1 selected" << endl;
for(int i=0;i<10;i++)
{
if(number[i]>temp)
temp=number[i];
}
cout << "The biggest number is: " << temp << endl;
break; //get out of switch block
case 2 : cout <<"Option 2 selected" << endl;
cout << "the sum of first and last number is :" << number[0] + number[9] << endl; //adding array 1 and 10
break; //get out of switch block
case 3 : cout <<"Option 3 selected" << endl;
x = number[0];
number[0] = number[1];
number[1] = x; //swap 1 and 2
x = number[2];
number[2] = number[3];
number[3] = x; //swap 3 and 4
x = number[4];
number[4] = number[5];
number[5] = x; //swap 5 and 6
x = number[6];
number[6] = number[7];
number[7] = x; //swap 7 and 8
x = number[8];
number[8] = number[9];
number[9] = x;//swap 9 and 10
for(int i=0;i<10;i++)
cout << " The new arrays are : " << number[i] << " " <<endl; //show new array
break; //get out of switch block
case 4 : cout <<"Option 4 selected" << endl;
for(int i=0;i<10;i++)
cout << "The values are : " << number[i] << " " << endl; //show the numbers;
break; //get out of switch block
default : cout <<"Invalid choice" << endl;
}
}
}
cout << "End of programme" << endl;
}
If you only want the text "The values are " to appear once, pull that out of the for loop. And if all the numbers should be on the same line, pull the << endl out of the for loop as well.