Please help with Arrays and Bool

This is a discussion on Please help with Arrays and Bool within the C++ Programming forums, part of the General Programming Boards category; Hello, Im new to this forum and I really need help with this newbie problem. I've been struggling with this ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    11

    Question Please help with Arrays and Bool

    Hello,
    Im new to this forum and I really need help with this newbie problem. I've been struggling with this assignment for very long now. I'm not even sure how to start the code right, as well as the structure of it. My code is a complete mess, and Im not sure what I'm brainstorming here. Any help is appreciated. Here's whats being asked of me:

    In a loop your program should do the following (you can use a do/while):

    receive as data input the weights (proportions) for the four exam grades, and put these into an array called weights.
    Read in 4 test grades into another array called grades.
    Use the following test data for one example:
    weights for exams: .10, .20, .25, .45
    first student record: 75, 87, 79, 92

    make up the rest yourself.
    call the function isitvalid() passing in the grades array, the function returns a bool (i.e. a boolean value).
    The function isitvalid() will determine whether or not the values in this array are valid. It will return true/1 for valid and false/0 if it's invalid. The function will return invalid if any grade in the array is not a valid grade (i.e. it's greater than 100 or less than 0). If the function returns false, your main program should read 4 values into the grades array again.
    You should calculate and print the final grade, which is the weighted average.
    Next your program will allow the user to see the any number of their test scores. For example. Your program can display a message such as: "If you don't believe the score is correct, how many of your grades would you like to see?", and it should call a function printarray() which takes two parameters, the array, and the number of grades the user would like to see..
    The function printarray() does not return anything, it will print out the number of grades the user would like to see. For example if the user passed the number 2, it will print the first two grades from the grades array.
    Your program will ask the user if s/he would like to enter another student.
    At the end of your program, you should print the class average i.e. the average of all the final grades combined.

    Here's what i got so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <math.h>
    using namespace std;
    bool isitvalid(double grades[]);  
    void printarray(); 
    int main(){
    double weights[4], grades[4];
    do
    {
    for (int i = 0; i < 4; i++){ 
    cout << "weight " << i << " :";
    cin >> weights[i];
    }
    for (int k = 0; k < 4; k++){
    cout << "grade: " << k ;
    cin >> grades[k];
    }
    }
    while (cin);
    }
    
    bool isitvalid(double grades[]){
           int k;
           bool cond = 0;
           if (grades[k] >= 0 && grades[k] <= 100){
             cond = 1;
             }else{
           cond = 0;
    return cond;
    }
    }
    }
    void printarray() 
    cin >> k
    Yes, evidently Im very confused. Please help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The math header should be
    Code:
    #include <cmath>
    Also, inside isitvalid(), you need to loop through k=0 to k=3. If any one of the grades is invalid, you can immediately return false (from inside the loop). If you finish the loop, then return true.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    Ok thank you, but which parameters should I use in order for main to call the function properly and where do I need to put the function in the main for it to be actually checking the grades. It also has to go back to intitializing grades[] if false, how will my logic look for it to work this way?
    Here's what I got so far, but Im not sure what its doing. Plus in this configuration it says " [Warning] the address of `bool isitvalid(double*)', will always evaluate as `true' ".

    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std;
    bool isitvalid(double grades[]);  
    void printarray(); 
    int main(){
    double weights[4], grades[4];
    
    do
    {
    for (int i = 0; i < 4; i++){ 
    cout << "weight: ";
    cin >> weights[i];
    }
    for (int k = 0; k < 4; k++){
    cout << "grade: ";
    cin >> grades[k];
    }
    }
    while(isitvalid);
    }
    
    bool isitvalid(double grades[]){
         int k;
         for (k = 0; k < 4; k++){
           if (grades[k] >= 0 && grades[k] <= 100){
             return true;
             }else{
           return false;
           }
    }
    }
    Can you please show me a primitive example of this piece of code just to guide me in the right direction. Thank you!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You can't call isitvalid() until after all the grades have been entered - right now you're calling it after each grade is entered. Also, inside isitvalid(), you have false and true backwards - you need to check each grade to see if it's INvalid, and if so return false - otherwise return true. Finally, you should indent consistently - say a fixed number of spaces or tabs per indentation level.

    Edit: I misunderstood how you're calling isitvalid() - it looks like you're checking your data after entering all of it, but it should be
    Code:
    while (!isitvalid(grades));
    since you only have to reenter the data if it's NOT valid.
    Last edited by robatino; 11-24-2006 at 08:48 PM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    Actually I made a mistake, it should check inside while loop and should look something like this:
    Code:
    for (int i = 0; i < 4; i++){ 
    cout << "weight: ";
    cin >> weights[i];
    }
    for (int k = 0; k < 4; k++){
    cout << "grade: ";
    cin >> grades[k];
    }
    isitvalid(grades);
    }
    while (cin);
    }
    But, my function is just not doing anything because its wrong. I dont understand when you say i have it backwards. I'm a newb to this so for me even putting right parameters is hard. Also I dont understand even if it works and returns "false" then how can I make it go back and cycle through grades again? I'd really appreciate it if you could take piece of my code and put it in right C++ context such as right parameters and proper logic in function so I could keep working on it. Thanks for all your help.

    Also I have another improperly structured function to attach that needs to ask which grades to print to screen from array here it is so far:
    Code:
    void printarray(double grades[k], int n){
          char more;
          int nums;
          while (more == 'y'){
     cout << "see amount of your grades? y/n" << endl;
     cin >> more;
     cout << "how many? ";
     cin  >> nums; 
     for (n = 0; n < nums - 1; n++){
      cout << grades[n];
    }
      return;
      }
     }   
    }
    then my code needs to find average of all grades in array.

    I spent 2 days including thanksgiving working on this code, please try to give me little bit more detail on how to put these 2 functions together with main. Looks like such a simple task and I cant even get it close to working.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    First, you really need to indent - your code is very hard to read. In isitvalid(), you return false if any one of the grades is false, otherwise return true. So the logic should look like:
    Code:
    for (k loop) {
      if (grades[k] is invalid) return false;
    }
    return true;
    So if any one of the grades is invalid, it returns false immediately. On the other hand, if they are all valid, you finish and exit the loop, and return true.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    You mean like this?
    Code:
    bool isitvalid(double grades[]){
         for (int g = 0; g < 4; g++){
             if (grades[g] < 0 && grades[g] > 100){
              return false;
               }else{
                return true;
         }
    }
    }
    It works but always returns as true. I think the problem is that Im not referencing my parameters correctly and its not actually checking my grades.
    Where does it go wrong?
    Here's the whole code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std;
    bool isitvalid(double []);  
    void printarray(double, int); 
    int main(){
    int g = 4;
    double average,count = 0, weights[4], grades[g];
    int n;
    do
    {
        for (int i = 0; i < 4; i++){ 
            cout << "weight: ";
            cin >> weights[i];
        }
        for (int k = 0; k < g; k++){
            cout << "grade: ";
            cin >> grades[g];
            count = count + grades[g];
        }
        average = count / g;
        cout << count << endl;
        cout << average << endl;
    }
    while (isitvalid(grades));
    }
    bool isitvalid(double grades[]){
         for (int g = 0; g < 4; g++){
             if (grades[g] < 0 && grades[g] > 100){
              return false;
               }else{
                return true;
         }
      }
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It should be "||", not "&&". Also, the "else" shouldn't be there, and the "return true;" should be _outside_ the loop. Remember that you can never return true until all grades have been validated, but you can return false if a single grade isn't valid. So "return false;" should be inside the loop, and "return true;" should be outside the loop. Take another look at my pseudocode above.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    I've done it the way you told me, but it still always returns it as "true".
    I think its because of my function parameter (double grades[]), its not checking the numbers in grades[] array in the main function.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The line should be
    Code:
    while (!isitvalid(grades));
    (note the exclamation point) since you stop the do loop when isitvalid(grades) is true. Also, the code in isitvalid should look like
    Code:
      for (int g = 0; g < 4; g++) {
        if (grades[g] < 0 || grades[g] > 100) return false;
      }
      return true;

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    thank you robatino I got the program working, now I will be posting another problem with string that I ran into.

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Make a new thread.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  3. Replies: 1
    Last Post: 04-20-2003, 05:02 PM
  4. Resizing Arrays in a Class
    By XSquared in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2002, 09:45 PM

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