Array Elements Issue..

I am having some issues with my code and i cannot figure it out how to fix it since I am a beginner in c++, can someone help me on how to fix the issue with my invert_Array and my Max array?

I am trying to invert the elements in my array and on the other get the max or bigger number in the elements.

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
#include <iostream>
using namespace std;

int populate_Array(double array[], int number);
void display_Array(double array[], int number);
void sum_Array(double array[], int number);
void invert_Array(double array[], int number);
void max_Array(double array[], int number);

int _tmain(int argc, _TCHAR* argv[])
{
	const int Number = 6;
	double total[6] = {};
	int number = populate_Array(total, Number);
	
	display_Array(total, number);
	sum_Array(total, number);
	invert_Array(total + 1, number - 2);
	max_Array(total, number);
	return 0;

	
}

int populate_Array(double array[], int number)
{
	double input;
	int i;
	for (i = 0; i < number; i++)
	{
		cout << "Please enter an integer: " << (i + 1) << ": ";
		cin >> input;
		array[i] = input;
	}
	cout << endl;
	return i;
}

void display_Array(double array[], int number)
{
	int sum = 0;


	cout << "The elements of the array are: ";
	for (int i = 0; i < number; i++)
	{
		cout << array[i] << ", ";
	}
	cout << endl;
	
}

void sum_Array(double array[], int number)
{
	double sum = 0;
	cout << "The sum of the elements: ";
	for (int i = 0; i < number; i++)
	sum += array[i];
	cout << sum;
	cout << endl;
}

void invert_Array(double array[], int number)
{
	double temp = 0;
	cout << "The reverse array is: " << temp;
	for (int i = 0; i < number / 2; i++)
	{
		temp += array[i];
		array[i] = array[number - i - 1];
		array[number - i - 1] = temp;
	}
	cout << endl;
}

void max_Array(double array[], int number)
{
	double max = 0;
	cout << "The biggest number is: ";

	for (int i = 0; i < number; i++)
	{
		if (number > array[i])
			array[i] = max;
		cout << max;
		cout << endl;
	
	}
}
Last edited on
closed account (48T7M4Gy)
For the max function, use the first element as the seed value for max.

so max = array[0] etc

Also, in line 83 it should be if ( array[i] > max ) etc

For the invert function, you need to write down exactly what you are trying to do because 'invert' can have a couple of diffent meanings. Is it just placing all the elements in reverse order?

I change the code in the max_Array and now it works perfectly! thanks!

in the invert function is just placing the elements in reverse order.
closed account (48T7M4Gy)
ok reverse order is not a lot different.

surely, i < number not number/2
如果是reverse array。
void invert_Array(double array[], int number)
{
double temp = 0;
cout << "The reverse array is: " << temp;
for (int i = 0; i <= number / 2; i++)
{
temp = array[i];// += is wrong
array[i] = array[number - i - 1];
array[number - i - 1] = temp;
}
cout << endl;
}


invert_Array(total , number);
here is my code, but still I only get 0 as an answer, it wont show the elements that the user input
1
2
3
4
5
6
7
8
9
       double temp = 0;
	cout << "The reverse array is: " << temp;
	for (int i = 0; i <= number; i++)
	{
		temp = array[i]; // deleted +=
		array[i] = array[number - i - 1];
		array[number - i - 1] = temp;
	}
	cout << endl;
closed account (48T7M4Gy)
but still I only get 0 as an answer


Of course that's what happens. Lines 1 and 2 set temp = 0 and print it out.

You need to print out the reversed array.

You need to add, after the reversal:
1
2
for (int i = 0; i <= number; i++)
   cout << array[i];


Last edited on
Topic archived. No new replies allowed.