Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I need help figuring this out please

I need help figuring this out please

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LilKoopa
    wrote on last edited by
    #1

    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; }

    L D 2 Replies Last reply
    0
    • L LilKoopa

      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; }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • L Lost User

        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

        L Offline
        L Offline
        LilKoopa
        wrote on last edited by
        #3

        thanks I cant believe I missed something so small

        1 Reply Last reply
        0
        • L LilKoopa

          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; }

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups