Need Help PLEASE

Good Morning all first of all i would like to say this is a great website, forum, specially if you can get help, Im not a programmer major, i think codeing is great,and im taking a c++ class, now i have been studying and searching watching tutorials, and i get the basics, but i received a homework and im lost,so i need some help, here is my code.

#include <iostream>
using namespace std;

int main () {
int sum=0;
const int arraysize =10;
int indexmax = 0 ;
int indexmin = 0 ;
int A[arraysize] = {87,68,94,100,83,78,85,91,76,87};

for (int i= 0; i < arraysize; i++) {
sum = sum + A[i];

}
int min = A [0];
for (int j= 1; j < arraysize; j++){
if (A[j] < min ) {
min= A[j];
indexmin = j;
}
}
int max= A [0];
for (int k=1;<arraysize; k++){
if (A [k] > max) {
max= A [k];
indexmax = k;
}
}
cout<<" sum is:" << sum << endl;
cout<<" average is: << sum/arraysize<<endl;
cout<<"minimum is: << indexmin<< endl;
cout<<"maximum is : <<indexmax<<endl;
}//end main


I understand the the program and what is doing finding all the max , min, average, and indexing the array, but now I was told to only use one for loop.
i need help because i see that there are 2 for loops,

greatly appreciated!
Currently, you have 3 for loops and if you look at them they are all doing the same thing being their counting through the array. So you could easily just use one of those for loops and have the program calculate what it needs through as it counts through.

2 other problem's I see

for (int k = 1;k < arraysize; k++) //<-- added k

and

1
2
3
4
	cout << " sum is:" << sum << endl;
	cout << " average is: "<< sum/arraysize << endl; //<-- added "
	cout << "minimum is: " << indexmin << endl; //<-- added "
	cout << "maximum is : " << indexmax << endl; //<-- added " 


One and one more problem your output is the position of the max number or min number and not the actual number. This can be easily fixed though Example:
1
2
cout << "minimum is: " << A[indexmin] << endl;
cout << "maximum is : " << A[indexmax] << endl;


Your code would look something like:
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
#include <iostream>
using namespace std;

int main() {
	int sum = 0;
	const int arraysize = 10;
	int indexmax = 0;
	int indexmin = 0;
	int A[arraysize] = { 87,68,94,100,83,78,85,91,76,87 };

	int min = A[0];
	int max = A[0];

	for (int i = 0; i < arraysize; i++)
	{
		sum = sum + A[i];

		if (A[i] < min)
		{
			min = A[i];
			indexmin = i;
		}
		if (A[i] > max)
		{
			max = A[i];
			indexmax = i;
		}
	}
	
	cout << "sum is:" << sum << endl;
	cout << "average is: "<< sum/arraysize << endl;
	cout << "minimum is: " << A[indexmin] << endl;
	cout << "maximum is : " << A[indexmax] << endl;
}
Last edited on
Thank Joe that was a really fast respond, like i said im not a major in programming, just what you are trying to say is that i could only use the first loop, that is doing the sum?

and the rest i can just write down what i need the program to do? like for example just delete the for statements?


and what do you mean by " <---- added"
Yes, u can just use your first for loop and move all the calculations in there you just have to change the variable you are using to count.

So ya, you would say something like I moved the if statement for max and min into to the for statement with sum. I then changed their counting variables to i to match the for loop then deleted the other for loops. I also moved the initializer for max and min to be above the first for statement.


I used //<-- added " to show I added a quotation mark to your code because you were missing a couple
Last edited on
Topic archived. No new replies allowed.