Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Designing a programme to find max and average of a set using C++

Options
  • 08-12-2008 4:54pm
    #1
    Registered Users Posts: 1,507 ✭✭✭


    I've been given an assignment in Uni to create a programme in C++ that can find the maximum and average of a set of values , inputted by the user. Now I've never done any C++ before so everythings a little new to me. I'm also at home today so I don't have visual c++ to test if everything's working properly. This is my code:
    #include <iostream.h>
    void main(void)
    int set = 0;
    {
    int set = 0;
    double one;
    cout << "Type the amount of numbers you would like to enter: ";
    cin >> set;
    if (set = 1)
    {
    cout << " Please enter one number. "
    cin >> one
    cout << " Your average number is " << double one << ".";
    cout << " Your maximum number must be " << double one << ", you've only entered one! " endl;
    break
    }
    else if ( set != 1 );
    cout << " Please enter " << set << " numbers. ";
    else
    cout << "\a";

    double number = 0;
    int count = 0;
    double max = 0;
    double total = 0;
    cin >> number
    for (count = 1; count <= set; count ++)

    {
    while (! good);
    {
    if (number >= 0 );
    good = 1
    else if ( number <= 0 );
    good = 1
    else cout << "\a";
    }
    if ( number <= max );
    max = max;
    else;
    {
    max = number;
    count << " Your new maximum number is " << max << ".";
    }
    total = total + number;
    if ( count = 1 );
    cout << " You have entered your 1st number. ";
    else if ( count = 2 );
    cout << " You have entered your 2nd number. ";
    else if ( count = 3 );
    cout << " You have entered your 3rd number. ";
    else
    cout << " You have entered your << count << "th number. ";
    }
    cout << " You have finished entering your set. ";
    cout << " Your maximum inputted value = " << max << ".";
    cout << " Your average value was " << total/set << "." endl;

    break;
    As you can see there's probably a fair few errors in there but I just can't see them yet. The line ( for (count = 1; count <= set; count ++) ) is causing me a small bit of grief. Will the programme run everything in the count 1 loop and then increase the count by 1, or will it increase the count first. This would mean that the line "if ( count = 1 ); cout << " You have entered your 1st number. "; would never be seen.

    Thanks for any help , I need as much as I can get :D


Comments

  • Registered Users Posts: 610 ✭✭✭nialo


    change if(count =1) to if (count == 1)

    same for the rest of your if statements.

    just glanced at the program so may be other issues..


  • Registered Users Posts: 1,507 ✭✭✭DamienH


    cool will do thanks for the reply


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    if ( number <= max );
       max = max;
       else;
          {
             max = number;
             count << " Your new maximum number is " << max << ".";
          }
    
    max=max is doing nothing
    Your else statement is ended before it does anything so what you have meant to be inside it is actually always going to run.
    if (set = 1)
    {
    cout << " Please enter one number. "
    cin >> one
    cout << " Your average number is " << double one << ".";
    cout << " Your maximum number must be " << double one << ", you've only entered one! " endl;
    break
    }
    

    break statement is doing nothing there, the if statement is always going to end there anyway
    void main(void)
    int set = 0;
    {
    int set = 0;
    

    You have set declared twice, should be only declared once and within the main function or in a header file.
    else if ( set != 1 );
    cout << " Please enter " << set << " numbers. ";
    else
    cout << "\a";
    

    That last else statement there will never run, since set is either equal to 1 or not equal to one. There is no third way for it to be:)

    You should report it using code tags with the fixed mistakes to make it easier to reaD:)


  • Registered Users Posts: 1,507 ✭✭✭DamienH


    // kjkhkj.cpp : Defines the entry point for the console application.
    //
    
    // sdfasfa.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream.h>
    
    void main(void)
    {
    int set = 0;
    double one;
    double number = 0;
    int count = 0;
    double max = 0;
    double total = 0;
    cout << "Type the amount of values you would like to enter: ";
    cin >> set;
    	{
    	if (set == 1)
    		{
    		cout << " Please enter one value. ";
    		cin >> one;
    		cout << " Your average value is " << one << "."<< endl;
    		cout << " Your maximum value must be " << one << ", you've only entered one! " << endl;
    		}
    	else 
    		cout << " Please enter your first of " << set << " values. ";
    
    	}
    	for (count = 1; count <= set; count ++ )
    		{
    		cin >> number;
    		total = total + number;
    
    				cout << " Please enter your number " << count + 1 << " of " << set << " values." << endl;
    			
    			if ( number > max )
    				{
    				max = number;
    				cout << " Your new maximum number is " << max << "." << endl;
    				}
    			else 
    				max = max;
    			
    		}       
    
    
    cout << " You have finished entering your set. "<< endl;
    cout << " Your maximum inputted value = " << max << "."<< endl;
    cout << " Your average value was " << total/set << "." << endl;
    
    }
    
    

    This is my corrected version, tested it today and everything seems to be working okay. Thanks for the help


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Why bother having the final else statement, it does nothing. I'm guessing your understanding is, if there is an if, you require an else. Not the case. Better to remove it.


  • Advertisement
  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    If you are programming in C++, then change

    #include <iostream.h>
    void main(void)

    to

    #include <iostream>
    int main()

    and return 0; at the end of main.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Btw, you watch "programmes" on TV, and you program in C++.


  • Registered Users Posts: 1,823 ✭✭✭EvilMonkey


    DamienH wrote: »
    I've been given an assignment in Uni to create a programme in C++ that can find the maximum and average of a set of values , inputted by the user. Now I've never done any C++ before so everythings a little new to me. I'm also at home today so I don't have visual c++ to test if everything's working properly.
    If your working from home you can download the Express edition of visual studio.


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    And if you're a student you get a free copy: www.dreamspark.com


Advertisement