I need help figuring this out please
-
I was wondering if someone could look at my code and tell me why when i try to get the average of the array it wont give me the average. It compiles, the only problem is the sum and average are the same. here is my code // LanusArrays.cpp : Defines the entry point for the console application. // Corey Lanus // Test 2 Part 2 // Lanus Arrays // Does a lot of things dealing with arrays. // October 16, 2006 #include "stdafx.h" #include #include using namespace std; int main() { const int ARRAY_SIZE = 20; int numbers[ARRAY_SIZE]; int count; ifstream inputFile; inputFile.open("numbers.txt"); for (count = 0; count < ARRAY_SIZE; count++) inputFile >> numbers[count]; inputFile.close(); int total = 0; for (int count = 0; count < ARRAY_SIZE; count++) total += numbers[count]; cout << "The sum of the numbers is: "<< total << endl; cout << endl; double total2 = 0; double average; for (int count = 0; count < ARRAY_SIZE; count++) total2 += numbers[count]; average = total2 / ARRAY_SIZE; cout << "The average of the numbers is: "<< total2 << endl; cout << endl; int highest; highest = numbers[0]; for (count = 1; count < ARRAY_SIZE; count++) { if (numbers[count] > highest) highest = numbers[count]; } cout << "The highest number is: " << highest << endl; cout << endl; int lowest; lowest = numbers[0]; for (count = 1; count < ARRAY_SIZE; count++) { if (numbers[count] < lowest) lowest = numbers[count]; } cout << "The lowest number is: " << lowest << endl; cout << endl; cout << "The numbers are: "; for (count = 0; count < ARRAY_SIZE; count++) cout << numbers[count] << " "; cout << endl; cout << "The numbers are:\n "; for (count = 0; count < ARRAY_SIZE; count++) cout << numbers[count] << " \n"; cout << endl; return 0; }
-
I was wondering if someone could look at my code and tell me why when i try to get the average of the array it wont give me the average. It compiles, the only problem is the sum and average are the same. here is my code // LanusArrays.cpp : Defines the entry point for the console application. // Corey Lanus // Test 2 Part 2 // Lanus Arrays // Does a lot of things dealing with arrays. // October 16, 2006 #include "stdafx.h" #include #include using namespace std; int main() { const int ARRAY_SIZE = 20; int numbers[ARRAY_SIZE]; int count; ifstream inputFile; inputFile.open("numbers.txt"); for (count = 0; count < ARRAY_SIZE; count++) inputFile >> numbers[count]; inputFile.close(); int total = 0; for (int count = 0; count < ARRAY_SIZE; count++) total += numbers[count]; cout << "The sum of the numbers is: "<< total << endl; cout << endl; double total2 = 0; double average; for (int count = 0; count < ARRAY_SIZE; count++) total2 += numbers[count]; average = total2 / ARRAY_SIZE; cout << "The average of the numbers is: "<< total2 << endl; cout << endl; int highest; highest = numbers[0]; for (count = 1; count < ARRAY_SIZE; count++) { if (numbers[count] > highest) highest = numbers[count]; } cout << "The highest number is: " << highest << endl; cout << endl; int lowest; lowest = numbers[0]; for (count = 1; count < ARRAY_SIZE; count++) { if (numbers[count] < lowest) lowest = numbers[count]; } cout << "The lowest number is: " << lowest << endl; cout << endl; cout << "The numbers are: "; for (count = 0; count < ARRAY_SIZE; count++) cout << numbers[count] << " "; cout << endl; cout << "The numbers are:\n "; for (count = 0; count < ARRAY_SIZE; count++) cout << numbers[count] << " \n"; cout << endl; return 0; }
-
The following code
average = total2 / ARRAY_SIZE;
cout << "The average of the numbers is: "<< total2 << endl;should be
average = total2 / ARRAY_SIZE;
cout << "The average of the numbers is: "<< average << endl;Sohail
-
I was wondering if someone could look at my code and tell me why when i try to get the average of the array it wont give me the average. It compiles, the only problem is the sum and average are the same. here is my code // LanusArrays.cpp : Defines the entry point for the console application. // Corey Lanus // Test 2 Part 2 // Lanus Arrays // Does a lot of things dealing with arrays. // October 16, 2006 #include "stdafx.h" #include #include using namespace std; int main() { const int ARRAY_SIZE = 20; int numbers[ARRAY_SIZE]; int count; ifstream inputFile; inputFile.open("numbers.txt"); for (count = 0; count < ARRAY_SIZE; count++) inputFile >> numbers[count]; inputFile.close(); int total = 0; for (int count = 0; count < ARRAY_SIZE; count++) total += numbers[count]; cout << "The sum of the numbers is: "<< total << endl; cout << endl; double total2 = 0; double average; for (int count = 0; count < ARRAY_SIZE; count++) total2 += numbers[count]; average = total2 / ARRAY_SIZE; cout << "The average of the numbers is: "<< total2 << endl; cout << endl; int highest; highest = numbers[0]; for (count = 1; count < ARRAY_SIZE; count++) { if (numbers[count] > highest) highest = numbers[count]; } cout << "The highest number is: " << highest << endl; cout << endl; int lowest; lowest = numbers[0]; for (count = 1; count < ARRAY_SIZE; count++) { if (numbers[count] < lowest) lowest = numbers[count]; } cout << "The lowest number is: " << lowest << endl; cout << endl; cout << "The numbers are: "; for (count = 0; count < ARRAY_SIZE; count++) cout << numbers[count] << " "; cout << endl; cout << "The numbers are:\n "; for (count = 0; count < ARRAY_SIZE; count++) cout << numbers[count] << " \n"; cout << endl; return 0; }
When you get all this working the way you want, be sure and check out some of the STL algorithms. They can do a lot of this for you. For educational purposes, it's always nice to do everything yourself (I started this way and am better for it), but just know that standard functions and classes do exist for some things.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch