Jump to content
  • Advertisement
Sign in to follow this  
bluefox25

my first real program!!

This topic is 4043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my first real program I have written on my own that was not part of a project or assignment. It calculates my final grade for my class. I compileed it and it works!! Can you guys make any suggestions for improvements? Shortcuts? etc....
#include <iostream>
/**
* Sets test scores
*@param exams[] contains the number of exams
*@pre Initialized by the calling program 
*@post Prompts user for score per test
*@returns a filled array with test scores
*/
double totaltest(double exams[]);
/**
* Sets quiz scores
*@param quizzes[] contains the number of quizzes
*@pre  Initialized by the calling program 
*@post Prompts user for score per quiz
*@returns a filled array with quiz scores
*/
double totalquiz(double quizzes[]);
/**
* Sets homework assignments  scores
*@param homework[] contains the number of homework assignments
*@pre Initialized by the calling program 
*@post Prompts user for score per homework
*@returns a filled array with homework assignment scores
*/
double totalhwork(double homework[]);
/**
* Sets Programming Projects  scores
*@param projs[] contains the number of programming projects
*@pre Initialized by the calling program 
*@post Prompts user for score per programming project
*@returns a filled array with programming project  scores
*/
double totalprojects(double projs[]);
/**
* Calculates the Final Grade
*@param int a contains the combined test scores
*@param int b contains the combined quiz scores
*@param int c contains the combined homework assignment scores
*@param int d contains the combined projects scores
*@pre Initialized by the calling program 
*@post Adds up all totals
*@returns The final grade for the course
*/
double calculate(double& a, double& b, double& c, double& d);//calculates grade
int main(){
    double test[4];
    double quiz[9];
    double assignments[12];
    double programmmingProjects[3];
    double tscore= totaltest(test);
    double qscore= totalquiz(quiz);
    double hscore= totalhwork(assignments);
    double pscore= totalprojects(programmmingProjects);
    double myGrade = calculate(tscore,qscore,hscore,pscore);
    
    std::cout << "My final grade for the course is: " << myGrade 
              << std::endl; 
   
    
    system("pause");
    return 0;
}
double totaltest(double exams[]){
    
    for(int i=1;i< 4;i++){
        std::cout << "Enter your score for Test " << i << ": "; 
        std::cin >> exams;
    }
    return exams[1] + exams[2] + exams[3];
}


double totalquiz(double quizzes[]){
        for(int i=1;i<9;i++){
        std::cout << "Enter your score for Quiz " << i << ": "; 
        std::cin >> quizzes;
    }
    return quizzes[1] + quizzes[2]+quizzes[3]+quizzes[4]+quizzes[5]+quizzes[6]
           +quizzes[7]+quizzes[8];
}

double totalhwork(double homework[]){
    for(int i=1;i<12;i++){
        std::cout << "Enter your score for Assignment " << i << ": "; 
        std::cin >> homework;
    }
    return homework[1] + homework[2]+ homework[3]+homework[4]+homework[5]
           +homework[6]+homework[7]+homework[8]+homework[9]+homework[10]
           +homework[11];
}

double totalprojects(double projs[]){
    for(int i=1;i<3;i++){
        std::cout << "Enter your score for Programming Project " << i << ": "; 
        std::cin >> projs;
    }
    return projs[1] + projs[2];
}
double calculate(double& a, double& b, double& c, double& d){
       using namespace std;
       cout.setf(ios::fixed);
       cout.setf(ios::showpoint);
       cout.precision(2); 
       
       return ((a/300)*.60) + ((b/80) *.10) + ((c/110) *.10) + ((d/200) *.20);
}



Share this post


Link to post
Share on other sites
Advertisement
You have a nasty bug in your code. Array indices start from 0, not 1.
your exams array is accessed at indices 1, 2 and 3, but since it's an array of size 3, it only has indices 0, 1 and 2. Only reason the program works for you is... luck. Apparently there isn't anything vitally important following exams[2], so while you write outside your array, the program doesn't crash.

You might want to use the standard library's vector instead of arrays. Among other things, they give you proper bounds checking, so you can easily spot these things.

Anyway, grats on your first program [grin]

Share this post


Link to post
Share on other sites
Is it a bug? I thought you could start at any index in array as long as you don't go over the size of the array. I used test[4] as the argument passed into the functions. If I started at inex 0 then it would read
"Enter score for Test 0:" or whatever the text is, and I wanted it to say Test 1,test 2, etc with the for loop. My book(Problem Solving with C++) sets the i to 1 on many occasions.

How can I make it crash? What should I input, Id like to see the error occure for learning purposes

Share this post


Link to post
Share on other sites
The error *is* occurring. Just you can't easily see since it's writing to essentially a random piece of memory. If you were lucky, it would try to write to some place that you don't have permission to write (eg: OS stuff) and would give you an Access Violation error (or similar depending on platform, etc.)

You have to start at index of 0. There's no ifs-ands-or-buts to that. Well, of course, unless you don't want to read the first thing in the array.

Share this post


Link to post
Share on other sites
Quote:
Original post by bluefox25
"Enter score for Test 0:" or whatever the text is, and I wanted it to say Test 1,test 2, etc with the for loop. My book(Problem Solving with C++) sets the i to 1 on many occasions.

what about

for(int i=1;i< 4;i++){
std::cout << "Enter your score for Test " << i+1 << ": ";
std::cin >> exams;

:)?

Share this post


Link to post
Share on other sites
Quote:
The error *is* occurring. Just you can't easily see since it's writing to essentially a random piece of memory. If you were lucky, it would try to write to some place that you don't have permission to write (eg: OS stuff) and would give you an Access Violation error (or similar depending on platform, etc.)


Well he should be be using 0 as a matter of practice but no "error" is occuring as he is only writing to or reading from item 11 at the most. It would however make sense to use item 0 as that is about 8 bytes of memory wasted in each array (though thats hardly the end of the world).

Next time try to create the array of size 11 and then use


for(int i=0;i<11;i++){
std::cout << "Enter your score for Test " << "note the plus 1 so it displays correctly"i+1 << ": ";
std::cin >> exams;
}



But yeah other than that you have no memory overwrite so dont panic. Just consider situations where you may waste memory.

Good Luck and have fun

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
The error *is* occurring. Just you can't easily see since it's writing to essentially a random piece of memory. If you were lucky, it would try to write to some place that you don't have permission to write (eg: OS stuff) and would give you an Access Violation error (or similar depending on platform, etc.)

You have to start at index of 0. There's no ifs-ands-or-buts to that. Well, of course, unless you don't want to read the first thing in the array.


You don't have to start at 0. E.g.: If you call malloc(), it does *not* necessarily return the address of the just allocated memory, instead it allocates the requested memory + size of the mem-descriptor, so free() can use a negative index on your new memory to access the memory descriptor.

I *don't* recommend arbitrary indices, that all messes up (at work we use delphi, and it is a mess, really), nevertheless you can use them (pseudo code):

void* myMalloc( size, startIndex )
{
void *ret = malloc( size );
return ret-startIndex;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by greenhybrid

void* myMalloc( size, startIndex )
{
void *ret = malloc( size );
return ret-startIndex;
}


Undefined behaviour. The mere action of creating a pointer outside the bounds of a memory segment will allow your program to act in an undefined fashion. The code above is perfectly allowed to format your hard drive or brutally reboot your computer. The bounds of the segment created by char* p = malloc(s) are between p and p + s inclusive (the C standard explicitly allows the programmer to manipulate past-the-end pointers).

Aside from that, you're doing pointer arithmetic with a void*, which is also a quite funny (if impossible) thing to do.

In general if someone needs special indices, he should either use an index-generation function, or write a subscript operator for a custom wrapper class.

Share this post


Link to post
Share on other sites
hi!
i think what can make your program good is to add question on how many test or work you have done so as to be able to calculate your true grade (of course school test are not just four you know).
also some error checking would e nice, like what if you entered a negative grade for example..

hope you find my advice useful ;)

Share this post


Link to post
Share on other sites

Here is one way you can make the function a bit more flexible:

double totaltest(double exams[], int nExams)
{
double total = 0.0;
for(int i=0; i<nExams; i++)
{
std::cout << "Enter your score for Test " << i+1 << ": ";
std::cin >> exams;
total += exams;
}
return total;
}


This way your function can deal with any number of exam results =)
You can do similar to the other functions as well, just make sure that the exams array you send to the function is big enough. (eg. at least nExams elements)

Grats with your first program by the way =)


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!