Can't get my C++ program to run!

This is a discussion on Can't get my C++ program to run! within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, Tried to take on the points everyone has passed onto me and have come up with the following ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Can't get my C++ program to run!

    Hi Guys,

    Tried to take on the points everyone has passed onto me and have come up with the following that:

    I Been asked to create a SIMPLE program with an array that holds values from 0 - 100. I then need to write functions to calculate:

    - largest number in the array
    - smallest number in the array
    - the sum of all of the numbers in the array
    - the mean of all of the numbers in the array

    So I created a file called values.h that I want to calculate (get) and show (display) the values on screen:

    Code:
    // values.h
    // Determining the largest, smallest, sum and mean
    # include <iomanip>
    //# include <iostream.h>
    //# include <iomanip.h>
    
    class values
      {
       public:
         int get_largest_number();
         int get_smallest_number();
         int get_sum_of_number();
         int get_mean_of_number();
         void display_largest_number() ;
         void display_smallest_number() ;
         void display_sum_of_number() ;
         void display_mean_of_number();
       protected:
         int largest_number;
         int smallest_number;
       };
    
    int values::get_largest_number(int a[])
    {
         largest_number = a[0];
         
         for(i=1; i<20; i++)
         { 
           if(a[i] > largest_number)
           {
                largest_number = a[i];
           }
         }
    } 
    
    int values::get_smallest_number(int a[])
    {
         smallest_number = a[0];
         
         for(i=1; i<20; i++)
         { 
           if(a[i] < smallest_number)
           {
                smallest_number = a[i];
           }
         }
    } 
    
    
    int values::get_sum_of_number(int a[])
    {
         int avg;
     
         avg = get_sum_of_number(a); 
         avg /= 20; 
         return(avg); 
    } 
    
    int values::get_mean_of_number(int a[])
    {
         int mean; 
         
         for(i=1; i<20; i++)
         { 
           mean += a[idx]; 
         }
         return(mean); 
    } 
    
    
    void values:: display_largest_number()
      {
      cout <<"The largest number is:" <<largest_number;
    }
    
    void values:: display_smallest_number()
      {
      cout <<"The smallest number is:" <<smallest_number;
    }
    
    void values:: display_sum_of_number()
      {
      cout <<"The sum of the numbers is:" <<avg;
    }
    
    void values:: display_mean_of_number()
      {
      cout <<"The mean of the numbers is:" <<mean;
    }
    Then get my main program to run it:

    Code:
    // ASSIGN77.CPP
    // A Program to display the largest, smallest, sum and mean
    #include "values.h"
    #include <stdlib>
    //#include "values.h"//
    //#include <stdlib.h>//
    void main ()
    	{
        values arithmetic;
        arithmetic.get_largest_number ();
        arithmetic.get_smallest_number ();
        arithmetic.get_sum_of_number ();
        arithmetic.get_mean_of_number ();
        arithmetic.display_largest_number ();
        arithmetic.display_smallest_number ();
        arithmetic.display_sum_of_number ();
        arithmetic.display_mean_of_number ();
        system ("pause");
    	}
    ...but it just wont run - any ideas please?

    Ta

    Chris

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Never, ever post code and only say: "it won't run, help!

    What errors are you getting? Mark the errors in your code with comments on the line they occur. Here is one:
    Code:
    int values::get_largest_number(int a[])
    {
         largest_number = a[0];
         
         for(i=1; i<20; i++)
         { 
           if(a[i] > largest_number)
           {
                largest_number = a[i];
           }
         }
    }
    You declared that the function will return an int. Does it? Hint: there's not even a return statement.

    Here's another:
    Code:
    int values::get_mean_of_number(int a[])
    {
         int mean; 
         
         for(i=1; i<20; i++)
         { 
           mean += a[idx]; 
         }
         return(mean); 
    }
    What is idx? It's not a class member name, it's not a function parameter name, and it isn't a variable name declared in the function.

    Your strategy for writing all programs should be this:

    1) Declare the class
    2) Write one member function and test it until your program compiles and the function works correctly. You will need to write some code in main() that calls the function to make sure it works.
    3) Move onto the next function and repeat.

    For now, you should comment out every function defintion in your file except the first one and follow those steps. Then uncomment the next function, and test that to make sure it works, etc.
    Last edited by 7stud; 11-25-2005 at 08:07 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    int values::get_mean_of_number(int a[])
    {
         int mean; 
         
         for(i=1; i<20; i++)
         { 
           mean += a[idx]; 
         }
         return(mean); 
    }
    And mean isn't initialized.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 06:32 PM
  3. Replies: 3
    Last Post: 07-11-2005, 04:07 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. Cannot run program in background?
    By registering in forum Linux Programming
    Replies: 3
    Last Post: 06-16-2003, 06:47 AM

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