Something I need help with is importing data. I can't really grasp how to do it very well.
The assignment I have to do is
Spoiler
[SIZE=3]For research purposes and to better help students, the admissions office of your local college wants to know how well female and male students perform in certain courses. You receive a file that contains female and male student GPAs for certain courses. The letter code f is used for female students and m for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places.
Sample output: Processing grades. f 3.40 f 4.00 m 3.56 m 3.80 f 2.30 f 3.95 m 3.90 m 4.00 m 2.00 f 4.00 f 2.80 m 3.70 m 2.98 f 3.89 m 4.00 f 3.90 m 1.90 m 2.90 f 1.50 f 2.67 m 3.80 m 2.35 f 2.90 f 3.70 f 4.00 m 3.78 m 4.00 f 3.98 Sum female GPA = 46.99 Sum male GPA = 46.67 Female count = 14 Male count = 14 Average female GPA = 3.36 Average male GPA = 3.33 [/SIZE]
He gave us a notepad file called Ex11Data.txt that has all of that info in it. Now I could do the assignment without importing the data (by just entering it all manually into the program itself), but I won't learn anything by doing it that way. If anyone could explain the best way to import the data that would be great.
Another thing I have trouble understanding is how to make use of object oriented programming. I guess I just need to practice more with it. The last time I had a class on it was a few months ago and I kind of forgot it =P. I know an object is something that is kind of universal to the program and can be moved around, but I'm having a bit of trouble creating one properly and making good use of it/knowing when to use it.
If you need to deal with data from an external source, you'll need the fstream class. This class allows you to use the ifstream and ofstream functions - the first to read files and the other to write to them.
cout<<"Sum of the male grades : "<<malegrades<<endl;
cout<<"Sum of the female grades : "<<femalegrades<<endl;
cout<<"Male count : "<<malecount<<endl;
cout<<"Female count : "<<femalecount<<endl;
cout<<"Male GPA average : "<<maleaverage<<endl;
cout<<"Female GPA average : "<<femaleaverage<<endl;
system("pause");
return 0;
}
runs just fine but you have to type all of the stuff manually (dunno if that's what he wanted to be honest).
and I have NO IDEA what ifstream and ofstream are lmao. I'm so noob at this ="(
This is the next assignment I'm working on now
Spoiler
1.Write a program to display five random addition problems on the screen. Sample output:
What is the sum of 5 + 7? 12 Correct!
What is the sum of 3 +8?11 Correct!
What is the sum of 7 + 9? 13 Sorry, the sum is 16.
What is the sum of 8 + 6? 14 Correct!
What is the sum of 9 + 8? 17 Correct!
2.Write a program to convert a temperature from Fahrenheit to Celsius (using three void functions) #include <iostream> #include <iomanip> using namespace std;
//function prototypes int main() { //declare variables int fahrenheit = 0; double celsius = 0.0;
//get input item
//calculate Celsius
//display output item
return 0; } //end of main function
//*****function definitions*****
man I suck at functions too -_-"
What I think I'm going to do for the random addition problems is make a randomizer spit out 10 numbers, and store those 10 numbers into 10 variables. Then just do like number1+number2 = answer.
could someone explain how to call out random numbers from an array? That would definitely be useful
Edit- I'm having trouble getting my randomizer to give me different numbers. This is me playing around with my randomizer, but it's spitting out the same number for randnum1 and randnum2
Code:
#include <iostream>
using namespace std;
int random();
int main()
{
int randnum1, randnum2;
randnum1 = random();
randnum2 = random();
cout<<randnum1<<endl;
cout<<randnum2<<endl;
system("pause");
return 0;
}
int random()
{
int number;
srand ( time(NULL) );
number=(rand()%11);
return (number);
}
Eos, it's \r\n, not \n\r. At any rate, check the newlines in the file.
An alternate option if you're trying to optimize disk usage would be to use strtok after reading the whole file into memory. Then with the string returned from strtok, use sscanf. Eos's way can read a file of up to whatever your file system can handle. Mine trades memory consumption for speed.
To increase clarity, you can also use a struct.
Code:
struct Scores
{
int count;
double total;
}
Then in the function.
Code:
int main(void)
{
struct Scores Men;
struct Scores Women;
...
}
Then in the loop:
Code:
switch ( gender )
{
case 'm':
Men.scores += score;
Men.count++;
break;
case 'f':
Women.scores += score;
Women.count++;
break;
}
I like how simplified your code is Fiel. I'm trying to learn to write as little code as I can and what you wrote is pretty nice. Is struct and object programming the same thing? It looks pretty similar.
Oh ok I got it. Objects and functions are definitely my weak point at the moment. I should get plenty of practice (i think) with the other assignments I have to do. That randomizer is being a peach though lol.
OOP is meant to follow natural language by letting code mimic the real world.
An object is any thing that you can dream up. Think of a notebook for example. A notebook is an object. An object is merely a thing with which you can perform actions on it. So you can read a notebook, write in the notebook, and update a notebook. These actions are of a "can" relationship with the notebook (you CAN read a notebook [Notebook.Read()], you CAN write in it [Notebook.Write()], you CAN update it [Notebook.Update()]). All "can" relationships are functions within the object.
Then you have "has a" relationships. So a notebook has a number of pages, it has a current page opened, it has a spiral binding, and it has a front cover. All of these "has a" relationships are variables within the object (int, short, string). In the struct shown above, each Score struct "has a" count and "has a" total.
The idea of OOP is data hiding. Ideally, the only way you should interact with an object is by sending commands to the object and never change its variables directly.
An object defines an abstract concept. For example, in the object definition you define what every notebook should look like. But the certain one on your table is one instance of that object. Every notebook you come across is a different instance of that object. Then you can change the state of each notebook by sending commands to its instance. Like in the real world, you can update the notebook on your desk without updating every single notebook in the universe.
Then you have "is a" relationships. This defines inheritance. So a diary is a kind of notebook. A daily planner is a kind of notebook. Inheritance specifies a more explicit type of your object. You can then inherit all of the functions from Notebook (read, write, update) into the diary object, but also include specific functions to make working with diaries easier (getCurrentDate(), WriteJournalEntry(), setCurrentMood())
If you still feel a bit lost, check out the Java introduction to OOP. Pretty much everything there will translate to any OOP language you encounter. The names might be slightly different, though.
EDIT: Nail down Objects ASAP. If you don't, you will be brutally destroyed by the time you start learning about OOP patterns.
In this case, you could have the object Student, with 2 properties - sex and gpa.
Of course, you can add other variables if you wanted (age, height, etc.) but in this case those 2 are all we really know.
Then when you read the input file, for each line you create a new instance (like creating a new variable) and set its properties according to the data.
I'm less familiar with C++ but in Java, when you create an object, you can implement default "interfaces" which means you give the object a certain set of functions that behave in a pre-specified manner. Such as print() returning a string describing the object, or compareTo(object) telling you which has a larger value. Once you implemented these functions you can treat your new object just like the primitive types (int, char, double, etc.) in your code, sticking them in lists, adding/subtracting them, printing, etc.
Eg. if you had sex(char) in your Student class, returning true if sex==char and false if sex!=char, you could write if(student.sex('m')) malegpa += student.gpa(); else femalegpa += student.gpa(); Then again, that's not really clear code - you'd more likely want to write getSex() which returns a char (m or f) and have if(student.getSex() == 'm').
Could someone help me out with my randomizer? I can't get it to spit out 10 different numbers and store those in 10 variables. The one I wrote just spit out 1 number and stored it in both randnums.
Its up in the 3rd post. I gotta make 5 different random math problems. I would store it in an array but I couldn't figure out how to make a loop that not only kept spitting out new numbers, but also stored those numbers into new variables.
int random()
{
int number;
[b]srand ( time(NULL) );[/b] // <--- THIS IS THE PROBLEM
number=(rand()%11);
return (number);
}
Your code runs so blindingly fast in C that all of it completes in less than a second. So you're seeding the PRNG with the same number for every function call. Call srand() in the main function before you call your own rand() wrapper.
LoL sweet. I'm glad it was an easy fix. I can't test it out right now since I'm not home (I'm on my cell phone).
The code I wrote was something I was using as a template sorta. Just to get a randomizer spittin out numbers and being able to store them. Making an array and making the math problems should be easy now.
First, convert your main() into a function like update_gpa(float gpa, int* gender_count, float* total_gpa). Basically, all this function does is updating the scores. Once you did that, recode your main() to call this function.
Once that's done, you will need to use a file stream reader (which I totally forgot the syntax for) and I think there's a function readline to read in a line in the file. As you can see, line[0] is the gender, and the rest is a float. Substring will do the trick here.
Your main() will be something like this:
for each line in the file, gender = line[0], gpa = convert to float(line[1..n])
if gender is m, pass in the gpa, the male total, the male gpa total into update_gpa
otherwise, pass in the gpa, the female total, the female total gpa into update_gpa
IMO, that's clean modular programming.
To make this OOP, firstly, you need a class calls GpaManager, which has 4 vars, male_total, female_total, male_total_gpa, female_total_gpa and perhaps 2 constants, MALE and FEMALE.
Then, you would need a public function calls update_gpa(char gender, &float gpa) and
protected function update_female_gpa(&float gpa)
protected function update_male_gpa(&float gpa)
Note that you're only going to read the GPA, so it's OK to pass the reference.
In update_gpa, it does the if statement and calls the male/female version of the function.
you can also do a toString() function for the class GpaManager, so that you can do something like cout << GpaManager.
That's pretty much does all the GPA logics.
Now, you can do something like a StdinGpaManager, which reads the data in from the std input and/or a FileGpaManager, which reads the data in from the file.
wow.... maybe trying to call the information from a file wasn't a good idea lol. It seems way more complicated than I thought it would be. I thought I could just do something like
call(insert file path here);
and that would be it. I guess I was wrong eh?
Well here is the code I have going for the rand function. I'm surprised it was so easy to write.
Code:
#include <iostream>
using namespace std;
int random();
int main()
{
int array[10];
int x;
srand ( time(NULL) );
for (x=0; x<10; x++){
array[x]=random();
cout<<array[x]<<endl;
}
system("pause");
return 0;
}
int random()
{
int number;
number=(rand()%11);
return (number);
}
gonna keep playing with it of course, but I got it spitting out random numbers and storing it into my array.
Given your capability to write code above, I dont see how hard it is to merge it with the 3rd post. There're minimal changes to your code to achieve what you wanted.
Tho, rather than mashing everything together into the main function, it is advisable to split it out to multiple. Like it or not, what you written are very primitive, and most likely wont be seeing it ever in the real world environment. Another thing I tend to see is that newbie programmers tend to let hell loose in their code. Try to maintain some disciplines with your code and it'll reward you greatly when you have to maintain it later. You may not know it, but even the best of us has written shitty code, and at later time, when we looked back at it, the reactions were "wtp?? i wrote this?" or "wtp is going on in here?"
That's why I suggested you tried modifying your code to something a bit more modular and more resusable by having a new function that handles the GPA logic. There is not much additional work in doing what I suggested either. As for OOP, well, take your time with that.