How do I go about reversing the numbers in an array to what they where imputed
Dec 4, 2015 at 4:51pm UTC
I am a bit stuck on how to approach reversing the order of an array so that for example i enter the numbers
1
2
3
4
5
into the array and they are outputted like
5
4
3
2
1
I am doing a menu where each option displays a certain task carried out on the array i.e option 1 displays the smallest number option 2 displays the average of the numbers in the array etc...
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#include "stdafx.h"
using namespace std;
int main()
{
int big, small, value, count = 0;
int list[12];
int option;
int sum = 0;
big = small = list[0];
cout << "Enter 12 numbers : " ;
for (int i = 0; i < 12; i++)
{
cin >> list[i];
}
do
{
cout << "\t\tMenu\n" ;
cout << "\t0.Display\n" ;
cout << "\t1.Total\n" ;
cout << "\t2.Average\n" ;
cout << "\t3.Largest\n" ;
cout << "\t4.Smallest\n" ;
cout << "\t5.Occurance of value\n" ;
cout << "\t6.Multiply values by number entred\n" ;
cout << "\t7.Reverse array\n" ;
cout << "\t99.Exit\n" ;
cout << "\t\t Option ? " ;
cin >> option;
switch (option)
{
case 0:
cout << "Contents\n" ;
for (int i = 0; i < 12; i++)
{
cout << list[i] << endl;
}
break ;
case 1:
cout << "Total" ;
cout << "\n\n" ;
for (int i = 0; i < 12; i++)
{
sum += list[i];
}
cout << sum;
break ;
case 2:
cout << "average" ;
for (int i = 0; i < 12; i++)
{
sum += list[i];
}
cout << setprecision(1) << fixed << sum / 12;
break ;
case 3:
cout << "Largest" ;
for (int i = 0; i < 12; i++)
{
if (list[i] > big)
{
big = list[i];
}
}
cout << big;
break ;
case 4:
cout << "Smallest" ;
for (int i = 0; i < 12; i++)
{
if (list[i] < small)
{
small = list[i];
}
}
cout << small;
break ;
case 5:
cout << "Occurances of value" ;
for (int i = 0; i < 12; i++)
{
cout << "Enter value to search for: " ;
cin >> value;
for (int i = 0; i<12; i++)
if (list[i] == value)
count++;
cout << value << " occurs " << count << " times" << endl;
}
break ;
case 6:
cout << "Enter value that you wish to multiply the contents \n in the array by : " ;
cin >> value;
for (int i = 0; i < 12; i++)
{
list[i] = value * list[i];
cout << list[i] << endl;
}
break ;
case 7:
break ;
case 99:
break ;
default :
cout << "Invalid option\n" ;
}
} while (option != 99);
return 0;
case 7 is the one im stuck on, not sure how to approach it, any help will be appreciated :)
Last edited on Dec 4, 2015 at 4:53pm UTC
Dec 4, 2015 at 4:59pm UTC
You already have a case for listing the contents of the array, just change that case code to do a reverse loop through the array:
1 2 3 4 5 6 7 8
case 7:
cout << "Reverse contents:\n" ;
for (int i = 11; i >= 0; i--)
{
cout << list[i] << endl;
}
break ;
Dec 4, 2015 at 5:10pm UTC
ahh i see, cheers for that mate :)
Dec 4, 2015 at 5:20pm UTC
BTW, your variable declarations is a bug lurking to strike:
1 2 3 4 5
int big, small, value, count = 0;
int list[12];
int option;
int sum = 0;
big = small = list[0];
You are assigning an uninitialized variable to two other uninitialized variables. I'd change it to something like this:
1 2 3 4 5 6 7
int big = 0;
int small = 0;
int value = 0;
int count = 0;
int list[12] = { 0 };
int option = 0;
int sum = 0;
list's initializer list assigns 0 to all 12 of list's elements.
Topic archived. No new replies allowed.