Like Tree1Likes
  • 1 Post By überfuzz

C++ Programing - Student Program with if statement, loops and histogram

This is a discussion on C++ Programing - Student Program with if statement, loops and histogram within the C++ Programming forums, part of the General Programming Boards category; HI, Im a beginner in C++, I was wondering if anyone could help me in the assignment? I need to ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Post C++ Programing - Student Program with if statement, loops and histogram

    HI, Im a beginner in C++, I was wondering if anyone could help me in the assignment? I need to make a code which would do the following;
    - get the marks from the user,
    -add them into the categories ( 0-25, 30-39, 40-69, 70-100)
    and i know that i need to make to a loop either a for or while.
    - and finally display them into a histogram,

    I have roughly made a pseudo code:

    Read in the marks

    get the first mark

    WHILE mark <= 100

    Add the mark to the category

    if mark is between 0 and 29
    Add 1 to category1 counter
    if mark is between 30 and 39
    Add 1 to category2 counter

    Count the number of marks

    Get the next mark

    Count the number of marks

    Display a histogram of all the categories

    display the range “0-29 “
    FOR each mark in category1
    Display a ‘*’

    Display the total number of students




    I would be very grateful for the help I'll receive from your comment. Thank you
    Last edited by raheelarehman; 11-25-2012 at 02:25 PM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Given the things I read between the lines you need to start with a hello world. From there it's a small step to set up some kind of data structure to store the results. Further, let the user put in new results.

    Keep us posted.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    @uberfuzz this is my code so far, but it still does not work, can you help me out here ? thanks


    Code:
    #include <iostream>
    using namespace std;
    int _main ()
    
    
    {
    	int mark = 0;  // Marks 
    	int cat_1 = 0;
    	int cat_2 = 0;
    	int cat_3 = 0;
    	int cat_4 = 0;
    
    
    	cout << "Please enter the marks the student has achived" << endl;
    	cin >> mark;
    
    
    		int i;
    		i = 0;
    		while (i<=10) {
    		
    				cout << i << endl;
    				i++;
    			cout << "works!!!" << endl; 
    
    
    	if ((mark >=0) && (mark <=25))
    	cout << "cat_1";
    
    
    else if ((mark >=30) && (mark <=39))
    	cout << "cat_2";
    
    
    else if ((mark >=40) && (mark <=69))
    		cout << "cat_3";
    
    
    else if ((mark >=70) && (mark <=100))
    	cout << "cat_4";
    }
    			return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    @uberfuzz this is my code so far, but it still does not work, can you help me out here ? thanks

    Code:
     
    Code:
    #include <iostream>
    using namespace std;
    int _main ()
    
    
    {
    	int mark = 0;  // Marks 
    	int cat_1 = 0;
    	int cat_2 = 0;
    	int cat_3 = 0;
    	int cat_4 = 0;
    
    
    	cout << "Please enter the marks the student has achived" << endl;
    	cin >> mark;
    
    
    		int i;
    		i = 0;
    		while (i<=10) {
    		
    				cout << i << endl;
    				i++;
    			cout << "works!!!" << endl; 
    
    
    	if ((mark >=0) && (mark <=25))
    	cout << "cat_1";
    
    
    else if ((mark >=30) && (mark <=39))
    	cout << "cat_2";
    
    
    else if ((mark >=40) && (mark <=69))
    		cout << "cat_3";
    
    
    else if ((mark >=70) && (mark <=100))
    	cout << "cat_4";
    }
    			return 0;
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Maybe you want to increment the counters of the category inside the loop. Do not forget to use brackets.

    Also every time you post do not forget to state why you are not happy with your code
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,302
    in your code, what happens to values of mark between 26 and 29?

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Code:
    if ((mark >=0) && (mark <=25))    
                cout << "cat_1";
    This does nothing to update the integer variables, this only displays "cat_1" on the screen. Instead, increment the variables with:

    Code:
    if((mark>=0) && (mark<=25))
                cat_1 = cat_1 + 1;
    OR

    Code:
    if((mark>=0) && (mark<=25))
                cat_1++;
    I'm not sure what the question is asking exactly as I feel you may have left out a few details, but if you were asked to create it in such a way that allows the user to input an indefinite amount of marks, use a do while loop instead.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Code:
     
    #include <iostream>
    using namespace std; 
    
    int main ()    
    {
        int mark = 0;  // Marks 
        int cat_1 = 0;
        int cat_2 = 0;
        int cat_3 = 0;
        int cat_4 = 0;    
        int i = 0;
    
        cout << "Please enter the marks the student has achived"; // ask the user to enter the mark 
        cin >> mark; // marks the student achived
    
        for (int i = 0, i<=10, i++)
        {
            if (mark >=0 && mark <=29)
            { cat_1 ++;
    
            cout << ("cat_1");  cat_1;
            }
    
            else if (mark >=30 && mark <=39);
            { cat_2 ++;
    
            cout << ("cat_2");  cat_2;
            }
    
            else if (mark >=40 && mark <=69)
            { cat_3 ++;
    
            cout << ("cat_3");  cat_3 ; 
            }
    
            else if (mark >=70 && mark <=100)
            { cat_4 ++;
    
            cout << ("cat_4");  cat_1 ;
            }
    
                system ("pause"); 
    
                return 0 ;
            }
    I've made some changes on my code so far but the for loop and the if statements still not seem to work, can someone help me out?

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Replace this
    Code:
    cout << ("cat_1");  cat_1;
    to this
    Code:
    cout << ("cat_1") << cat_1;
    Same for the other.

    The thing in your code is that cat_1; by it's own has no effect and only the "cat_1" is sent to cout
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    You may also want to look at arrays (std::array).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    How do i find the highest and lowest mark?
    I think i've made the statement but not sure where i should place it.. Thanks in advance
    Here is the statement:
    Code:
    {
                  if (mark == cat_3 || mark == cat_4) {
                        
    cout >> “pass”<< student_pass << endl;          }
                  else if (mark == cat_1 || mark == cat_2){
                         cout >> “fail” << student_fail <<endl;          }
           }
    here is my code :
    Code:
     
    #include <iostream>
    using namespace std;
    int main ()
    
    {
        // name the variables of catergories 
    
        int cat_1 = 0; // category 1 
        int cat_2 = 0; //  category 2
        int cat_3 = 0; // category 3
        int cat_4 = 0;    // category 4
    
        int i = 0; 
        int total_marks = 0; // total marks
        int counter = 0; // adding the amount of marks entered by user
        int average_mark = 0; // the average mark from all the marks put together
        int pass = 0;
            int fail = 0;
        //number of students passing 
        //highest mark 
        //lowest mark 
    
    
        int mark = 0;  // Marks entered by user 
        cout << "Please enter the marks the students has achived " << endl; // ask the user to enter the mark 
        cin >> mark; // marks the student achived from the user
    
        for (i = 0; i<=9; i++) // loop 
        {
    
            total_marks = total_marks + mark;
            counter++; //total marks
    
            if (mark >=0 && mark <=29){ 
                cat_1 ++;
            }
    
            else if (mark >=30 && mark <=39){ 
                cat_2 ++;
            }
    
            else if (mark >=40 && mark <=69){ 
                cat_3 ++;
            }
    
            else if (mark >=70 && mark <=100){ 
                cat_4 ++;
            }
            cin >> mark;
        }
    
    
    
        cout << "Number of students with marks 0-29|"; 
        for (i = 0; i<cat_1; i++) {
            cout << "*";
        }
        cout << endl;
    
        cout << "Number of students with marks 30-39|"; 
        for (i = 0; i<cat_2; i++) {
            cout << "*"; 
        }
        cout << endl;
    
        cout << "Number of students with marks 40-69|"; 
        for (i = 0; i<cat_3; i++) {
            cout << "*"; 
        }
        cout << endl;
    
        cout << "Number of students with marks 70-100|"; 
        for (i = 0; i<cat_4; i++) 
        {
            cout << "*"; 
        }
        cout << endl;
    
        cout << "The total amount of student marks is|" << counter << endl; // the number of student whose marks had been entered 
    
    
        for (i = 0; i<counter; i++) {
            average_mark = total_marks / counter;//average mark between all the makes entered
        }
    
        cout << "The average mark is|" << average_mark <<endl; 
    
    
    
    
    
            system ("Pause");
            return 0;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a histogram program
    By makonikor in forum C Programming
    Replies: 33
    Last Post: 04-17-2010, 01:17 AM
  2. Adding statement between loops
    By SilentPirate007 in forum C Programming
    Replies: 4
    Last Post: 03-01-2010, 03:55 AM
  3. loops in a main statement....
    By xero18 in forum C Programming
    Replies: 8
    Last Post: 10-30-2005, 08:36 PM
  4. Student Grade Program
    By Vinod Menon in forum C Programming
    Replies: 5
    Last Post: 05-28-2004, 02:49 PM
  5. Loops using while statement?????
    By bearcat19 in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 10:17 AM

Tags for this Thread


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