Find average using a vector keeps displaying nan

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1213 Views - Last Post: 12 January 2019 - 02:39 PM Rate Topic: -----

#16 albert003   User is offline

  • D.I.C Addict

Reputation: 22
  • View blog
  • Posts: 614
  • Joined: 15-December 14

Re: Find average using a vector keeps displaying nan

Posted 09 January 2019 - 11:05 PM

OOoops you're right. Can't believe I missed that parameter. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#17 albert003   User is offline

  • D.I.C Addict

Reputation: 22
  • View blog
  • Posts: 614
  • Joined: 15-December 14

Re: Find average using a vector keeps displaying nan

Posted 10 January 2019 - 10:27 AM

Ooooops you're right I missed that and it was right under my nose. Ok, I corrected that mistake. Thank you for pointing that out for me.

This is the program.

#include <iostream>
#include <string>
#include <vector>

typedef std::vector<double>grades;
grades getHomework()
{
    double input;
    int number;
    grades homework;
    std::cout << "How many grades will you enter?.\n" <<std::endl;
    std::cin >> number;

    for(int i = 0; i < number; i++)
    {
        std::cout << i+1 << " grade" <<std::endl;
        std::cin >> input;
        homework.push_back(input);
    }
    return homework;
}
std::string getName()
{
    std::string name;
    std::cout << "Enter your full name:" << std::endl;
    std::getline(std::cin,name);

    return name;
}
double getAverage(const grades& homework)
{
    double average;
    double total = 0;
    for(int i = 0; i < homework.size();i++)
    {
        total += homework[i];
    }

    average = total / homework.size();

    return average;
}
void showResults(const std::string &name,double average)
{
    std::cout << name << "'s average grade is:  "<< average << std::endl;
}

int main()
{
    double average;
    std::string name = getName();
    grades homework = getHomework();
    average = getAverage(homework);
    showResults(name,average);
}


Was This Post Helpful? 0
  • +
  • -

#18 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: Find average using a vector keeps displaying nan

Posted 10 January 2019 - 11:47 PM

Good. Now the functions look like they're each doing one thing only and their signatures make sense. In future, strive for design like this.

The only very minor complaint I have is about the splitting of the declaration and initialisation of the variable average in main (lines 50 and 53, respectively). Why not do both things in one line like the others?
Was This Post Helpful? 0
  • +
  • -

#19 albert003   User is offline

  • D.I.C Addict

Reputation: 22
  • View blog
  • Posts: 614
  • Joined: 15-December 14

Re: Find average using a vector keeps displaying nan

Posted 10 January 2019 - 11:57 PM

Honestly, I didn't know you could do it in one line. I always thought you had to declare the variable in one line and then use in in another line. Similar to what I did in line 32 and 39. I'll make that change and try to remember that for future programs.
Was This Post Helpful? 0
  • +
  • -

#20 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: Find average using a vector keeps displaying nan

Posted 11 January 2019 - 01:40 AM

View Postalbert003, on 11 January 2019 - 06:57 AM, said:

Honestly, I didn't know you could do it in one line.


This doesn't make any sense. Look at line 51, for example. You're declaring and initialising on one line there, aren't you?
Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg   User is online

  • member icon

Reputation: 5652
  • View blog
  • Posts: 17,353
  • Joined: 25-December 09

Re: Find average using a vector keeps displaying nan

Posted 11 January 2019 - 10:24 AM

Quote

I always thought you had to declare the variable in one line and then use in in another line. Similar to what I did in line 32 and 39.

This also doesn't make sense since it should also be declared and initialized in the same place. You really should study up on the

Quote

RAII
concept. In C++ variables should be defined as close to first use as possible and whenever possible initialize the variable at the same time.

double getAverage(const grades& homework)
{
//    double average; // Try to define variables closer to first use!
    double total = 0;
    for(int i = 0; i < homework.size();i++)
    {
        total += homework[i];
    }

//  double average = total / homework.size(); // If you must use a variable initialize it close to first use!

//    return average; // But in this case you don't even need this variable, just return the "average"!
    return total / homework.size();

}


But in this case, as pointed out in an earlier post, you don't even need that variable, just return the desired value.


By the way you should be careful of just taking code and pasting it into your programs without closely studying the copied code. If you would have closely studied the code you copied from an earlier post you should have realized that it possible to initialize your variables at the time of their definition since that copied code illustrated that concept.

For example your original code in main()
    grades homework;
    std::string name;
    double number,input,avg;
    int x = 0;
    std::cout << "What is the students first and last name:" <<std::endl;
    std::getline(std::cin,name);
    enter(homework,number,input,x);
    average(name,homework);
}


And the code you appear to have copied from one of the posts:

    std::string name = getName();
    grades homework = getHomework();


By the way since you seem to have come into some money recently, I suggest you consider buying a more current book instead of relying on the semi outdated free book you're currently using. But almost any book is better that the approach you had been taking.

Quote

I'm a visual person and understand better when I have examples in front of me.

By the way there is no such thing as a "visual person". You really need to try to overcome this limitation and learn research the written material you don't understand. In this age of information you are only a few clicks away from many different explanations of the issues you don't understand. But by all means, when dealing with programming, strive to stay away from videos as much as possible. IMO, programming howto videos are usually extremely poor choices when trying to learn a programming topic. They usually skip over important minor details and just as often are produced by programming novices with limited knowledge and rarely if ever actually tell you the whole story.

Jim
Was This Post Helpful? 0
  • +
  • -

#22 albert003   User is offline

  • D.I.C Addict

Reputation: 22
  • View blog
  • Posts: 614
  • Joined: 15-December 14

Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 10:05 AM

ndc85430 You're right I did do that previously. I'll make sure to initiate it at the same time I'm using it in my program.


jimblumberg I actually found the free PDF download for both books. I've been using that to study accelerated c++. You're right and I will work on that short coming.
Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg   User is online

  • member icon

Reputation: 5652
  • View blog
  • Posts: 17,353
  • Joined: 25-December 09

Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 10:12 AM

Quote

jimblumberg I actually found the free PDF download for both books.

Well I gave you those books as alternatives to more modern works because you said you were a poor student. But since you have now come into some money you should really consider buying a more modern book to learn modern C++ (C++14 or C++17) instead of C++98 with a little C++11, if you can afford a new laptop then surely you can afford a modern C++ book if you really want to learn C++.

Jim
Was This Post Helpful? 0
  • +
  • -

#24 albert003   User is offline

  • D.I.C Addict

Reputation: 22
  • View blog
  • Posts: 614
  • Joined: 15-December 14

Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 01:08 PM

Will do. I'll look on amazon and look for more recent books. Is there a particular author you recommend?.
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg   User is online

  • member icon

Reputation: 5652
  • View blog
  • Posts: 17,353
  • Joined: 25-December 09

Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 02:39 PM

Quote

Is there a particular author you recommend?

Not off the top of my head.

Edit Here is a good book list to start with.

Jim

This post has been edited by jimblumberg: 12 January 2019 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2