C+P code use to work but now it doesn't!

This is a discussion on C+P code use to work but now it doesn't! within the C++ Programming forums, part of the General Programming Boards category; Code: #include"stdafx.h" #define SIZE 10 #define LENGTH 10 #defineFormatLength 4 void ReadScore(float *, int); //by ref, by value void ReadMin(float ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    C+P code use to work but now it doesn't!

    Code:
    #include"stdafx.h"
    #define    SIZE 10
    #define    LENGTH 10
    #defineFormatLength 4 
    
    void ReadScore(float *, int);  //by ref, by value
    
    void ReadMin(float *);          //by ref, by name
    
    float ComputeAvg(float *, int); //by ref, by value
    
    float FindHighestScore(float *, int);
    
    float FindLowestScore(float *, int);
    
    int FindNum(float *, int, float); //by ref, by value, by value
    
    void Print(float *, int);       //by ref, by value
    
    void PrintResults(int, float, float, float, int, int);  //by value
    
    
    //ofstream f_cout("Project1_PartA.txt", ios::out);
    
    
    int main()
    {
    
    
    // Storage
    
    
      float Exam, avg, highest, lowest, Min_Pass, Min[5];
    
    
      int Num_Pass, Num_A;
    
    
    
    // INPUT
    
      ReadScore(Exam, SIZE);
    //by ref, by value
    
      ReadMin(Min);
    //by ref, by name
    
    
    
    // Process
    
      avg = ComputeAvg(Exam, SIZE);
    //by ref, by value
    
      FindHighestScore(Exam, SIZE);
    
      FindLowestScore(Exam, SIZE);
    
      Num_Pass = FindNum(Exam, SIZE, Min[5]);
    //by ref, by value, by value
    
      Num_A = FindNum(Exam, SIZE, Min[5]);
    
    
    
    //OUTPUT
    
      Print(Exam, SIZE);
    //by ref, by value
    
      PrintResults(SIZE, avg, highest, lowest, Num_Pass, Num_A);
    //by value
    
    
    
      return 0;
    
    }
    
    
    //INPUT
    
    
    void ReadScore(float *Ex, int n)  //by ref, by value
    {
    
    
      int i;
    
    
      for (i = 0; i < n; i++)
      {
    
        cout << "Enter grades: ";
    
        cin >> Ex[i];
    
      }
    
    }
    
    
    void ReadMin(float *min)        //by ref, by name
    {
    
      cout << endl << endl;
    
      cout <<
          "Enter the minimum score for an A, B, C, D, and passing: " << endl;
    
    
    
      for (int i = 0; i < 5; i++)
      {
    
        cin >> min[i];
    
      }
    
    
    }
    
    
    
    //Process
    float ComputeAvg(float *Ex, int n)  //by ref, by value
    {
    
    
      int i;
    
    
      float sum = 0;
    
    
      for (i = 0; i < n; i++)
      {
    
        sum = sum + Ex[i];
    
      }
    
    
      return (sum / n);
    
    }
    
    
    int FindNum(float *Ex, int n, float Min_P)  // by ref , by value, by name
    {
    
    
      int i, count = 0;
    
    
      for (i = 0; i < n; i++)
      {
    
    
        if (Ex[i] >= Min_P)
    
          count++;
    
      }
    
    
    
      return count;
    
    }
    
    
    €€
    // OUTPUT
    void Print(float *Ex, int n)    //by ref, by value
    {
    
    
      int i;
    
    
      for (i = 0; i < n; i++)
      {
    
    
        if (i % LENGTH == 0)
    
          cout << endl;
    
        cout << setw(FormatLength) << Ex[i];
    
      }
    
    }
    
    
    void PrintResults(int n, float av, float high, float low, int Num_P, int Num_A) // value
    {
    
      cout << '\n' << "Average Score is: " << av << endl;
    
      cout << "Highest is: " << high << endl;
    
      cout << "Lowest is: " << low << endl;
    
      cout << "Number Passed is: " << Num_P << endl;
    
      cout << "Number Failed is: " << n - Num_P << endl;
    
      cout << "Number with a A: " << Num_A << endl;
    
    }
    
    
    float FindHighestScore(float *Ex, int n)
    {
    
    
      float high = Ex[0];
    
    
    
      for (int i = 1; i < n; i++)
      {
    
    
        if (high < Ex[i])
    
          high = Ex[i];
    
      }
    
    
    
      return high;
    
    }
    
    
    €€float FindLowestScore(float *Ex, int n)
    {
    
    
      float low = Ex[0];
    
    
    
      for (int i = 1; i < n; i++)
      {
    
    
        if (low > Ex[i])
    
          low = Ex[i];
    
      }
    
    
    
      return low;
    }

    The problem is the FindHighestScore and FindLowestScore functions keep telling me that the variables "highest" and "lowest" are uninitialized but they were before when I tested it. By initializing them to 0, I get "Highest = 0" and "Lowest = 0".

    What's wrong with it?
    Last edited by Salem; 10-29-2012 at 11:52 PM. Reason: demunged the font/tag horror

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The title says
    Code:
    C+P code use to work but now it doesn't!
    but i see C. Maybe my screen makes an autoboxing with it from C++ to C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    In your readmin() function you are only looping through 4 values. Since the termination condition is that i is less then 5, and there are five possible scenarios, this makes sense. But logically when the loop counter reaches 5 it breaks, because the true condition is LESS THEN 5, and not less then or equal to 5.

    Consider changing the statement i < 5 to

    i <= 5 or i < 6

    Either case the the loop will continue through the 5 th item before breaking.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Try using this syntax I your find high and find low functions.

    FindHighestScore(float Ex[], int num)

    It's kinda the same thing, except that the compiler expects an array.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by pogrady
    Try using this syntax I your find high and find low functions.

    FindHighestScore(float Ex[], int num)

    It's kinda the same thing, except that the compiler expects an array.
    Err... it is exactly the same thing, except for spelling. The compiler expects a pointer either way, not an array, though we pass an array because the array is converted to a pointer to its first element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Thanks for all your feedback, but I simply am trying to get my program to output the Highest and Lowest Score. Currently, it only prints garbage values because it says that they are uninitialized; however, if I initialize "highest" and "lowest" to 0, the Highest and Lowest Score will both be zero. What can I do in order to get my program to output the correct highest and lowest values?

    I've done this before using the same code and it worked, but now it doesn't.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,694
    Assign a value to highest and lowest; I suggest using the functions you wrote.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Example Code doesn't work!
    By Kayl669 in forum C Programming
    Replies: 8
    Last Post: 02-18-2010, 09:19 AM
  2. why doesn't the code work on right part of tree?
    By lastrial in forum C Programming
    Replies: 1
    Last Post: 05-16-2007, 06:42 PM
  3. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 12:53 PM
  4. this code doesn't work please help me debug
    By newcstudent in forum C Programming
    Replies: 2
    Last Post: 10-09-2006, 05:01 AM
  5. this code compiles, but doesn't work how it should
    By Leeman_s in forum C++ Programming
    Replies: 10
    Last Post: 09-10-2002, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21