array problems

This is a discussion on array problems within the C++ Programming forums, part of the General Programming Boards category; Here is my assignment: Assignment #15 CSC190Web Spring 2002 __________________________________________________ ______________________ The purpose of this assignment is work with an ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    18

    array problems

    Here is my assignment:

    Assignment #15
    CSC190Web
    Spring 2002
    __________________________________________________ ______________________

    The purpose of this assignment is work with an array as well as writing several free functions.
    It is assumed that the array will store N random numbers. The value of N will be entered from the keyboard using a free function and must be limited to having a value in the range 1 through 50. N also represent how many random numbers will be stored in our array.
    __________________________________________________ ______________________

    You are required to write the free functions for the following program as well as execute these free functions to be assured that they are correct.



    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>

    const int MaxSize = 50;

    // The following are prototypes declaring functions invoked doing the
    // execution of function main.

    void HowManyRandomNumbers(int& N);
    void GenerateRandomNumbers( int A[], int N );
    int CountEvenNumbers( int A[], int N );
    void DisplayArray ( int A[], N);


    void main()
    {
    srand(time(0));

    // Create the array Arr:
    int Arr[MaxSize];
    int N, Count;

    // Have the user enter a count on the number of random numbers he/she
    // wishes to generate.
    HowManyRandomNumbers( N );

    // Store N random numbers in array Arr.
    GenerateRandomNumbers( Arr, N );

    // Count the number of even integers stored in array Arr.
    Count = CountEvenNumbers( Arr, N );

    // Display the N values stored in array Arr and the count on the number
    // of even integers stored in array Arr.
    DisplayArray(Arr, N);
    cout << "\n Count on even integers: " << Count << endl;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    here is what I've done:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    const int  MaxSize = 50;
    
    void main()
    {
    
    // The following are prototypes declaring functions invoked doing the
    // execution of function main.
    
    void  HowManyRandomNumbers(int& N);
    void  GenerateRandomNumbers( int A[], int N );
    int   CountEvenNumbers( int A[], int N );
    void  DisplayArray ( int A[], N);
    
    
    void main()
    {
        srand(time(0));
    
    // Create the array Arr:
        int   Arr[MaxSize];
        int   N, Count;
    
    // Have the user enter a count on the number of random numbers he/she
    // wishes to generate.
        HowManyRandomNumbers( N );
    
    // Store N random numbers in array Arr.
        GenerateRandomNumbers( Arr, N );
    
    
    // Count the number of even integers stored in array Arr.
        Count = CountEvenNumbers( Arr, N );
    
    // Display the N values stored in array Arr and the count on the number 
    // of even integers stored in array Arr.
        DisplayArray(Arr, N);
        cout << "\n Count on even integers: " << Count << endl;
    }
    
    // ------------------------------------------------------------------ //
    //                          Function Definitions                      //
    // ------------------------------------------------------------------ //
    
    // Purpose: This function asks for and inputs the amount of random numbers
    // that the user would like used in the program.
        void HowManyRandomNumbers(int& N)
    {
        cout << "How many random numbers would you like generated?";
        cin >> N >> endl;
    }
    
    // Purpose: This function generates random numbers that are assigned as
    // elements of array Arr.
        void GenerateRandomNumbers(int Arr[], int N )
    {
        for( int N = 0; N < MaxSize; ++N)
        {
            Arr[I] = rand() % 1000;
        }
    
    }
    
    // Purpose: This function counts and returnsthe even numbers inn array Arr.
        int   CountEvenNumbers( int Arr[], int N )
    {
    
            long  Even = 0;
    
        for( int N = 0; N < MaxSize; ++N)
        {
            if( Arr[N] % 2 == 0 )
            {
            Even ++;
            }
        }
    
        return Even;
    }
    
    
    // Purpose: This function displays the elements of array Arr.
        void DisplayArray (int Arr[], int N)
    {
        cout << "Array elements: " << endl;
        for(int N = 0; N < MaxSize; ++N)
        {
            cout << "Arr[" << N << "] = " << Arr[I] << endl;
        }
    Last edited by slamit93; 04-24-2002 at 03:22 PM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    if anybody has any idea what is still wrong, it would be greatly appreciated... I am screwed if I don't pass this class, and my grade is hurting a little bit as it stands... Please help me!

  4. #4
    Unregistered
    Guest
    in the first function defintion make sure input is in the desired range:


    Code:
        void HowManyRandomNumbers(int& N)
        {
           N = -1000;
           while(N < 1 || N > 50)
           {
               cout << "How many random numbers would you like 
                        generated?" << endl;
                cin >> N;
            }
         }

    In your other function definitions substitute a diferent value for N and use N in place of MAXSIZE. This allows you to only loop through as many values as used, not as many values as could be used. IF N is less than MAXSIZE these functions will do some weird things the way you have it.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void main()
    >{
    //You have void main() twice.  Take the first one out.
    
    >void  DisplayArray ( int A[], N);
    //Should be:
    void  DisplayArray ( int A[], int N);
    
    >void main()
    //This is wrong.  main() returns an int.
    int main(void)
    
    >    cin >> N >> endl;
    //No endl here
        cin >> N;
    
        >for( int N = 0; N < MaxSize; ++N)
        for( int I = 0; I < N; ++I)
    
        >for( int N = 0; N < MaxSize; ++N)
        for( int i = 0; i < N; ++i)
    
            >if( Arr[N] % 2 == 0 )
            if( Arr[i] % 2 == 0 )
    
        >for(int N = 0; N < MaxSize; ++N)
        for(int I = 0; I < N; ++I)

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    ok... here's what I've got now,
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    const  int  MaxSize = 50;
    
    // The following are prototypes declaring functions invoked doing the
    // execution of function main.
    void   GenerateRandomNumbers( int A[] );
    double CountEvenNumbers( int A[] );
    void   DisplayArray ( int A[], double Avg );
    void HowManyRandomNumbers(int& N);
    
    void main()
    {
    
        srand(time(0));
    
    // Create the array Arr:
        int     Arr[MaxSize];
        int  Count, N;
    
    // Have the user enter a count on the number of random numbers he/she
    // wishes to generate.
        HowManyRandomNumbers( N );
    
    // Initialize the array with random numbers.
        GenerateRandomNumbers( Arr );
    
    // Compute the average value for elements of array Arr.
        Count = CountEvenNumbers( Arr );
    
    // Display the values of array Arr and Average.
        DisplayArray(Arr, Count);
    // The following halts the program waiting for the
    // user to press the '"Enter Key" to quit.
        cout << "\n\n";
        cout << "Press Enter key to quit: ";
        cin.ignore(80, '\n');
        cin.get();
    
    }
    
    // ------------------------------------------------------------------ //
    //                          Function Definitions                      //
    // ------------------------------------------------------------------ //
    
    // Purpose: This function asks for and inputs the amount of random numbers
    // that the user would like used in the program.
        void HowManyRandomNumbers(int& N)
        {
           N = -1000;
           while(N < 1 || N > 50)
           {
               cout << "How many random numbers would you like generated?" << endl;
                cin >> N;
            }
         }
    
    
    void GenerateRandomNumbers(int A[])
    {
    // Purpose: This function generates random numbers that are assigned as
    // elements of array A.
        for( int I = 0; I < MaxSize; ++I)
        {
            A[I] = rand() % 50;
        }
    
    }
    
    
    double CountEvenNumbers(int A[])
    {
    // Purpose: This function counts the number of even values in array A.
    
        long  Even = 0;
    
        for( int I = 0; I < MaxSize; ++I)
        {
            if(A[I] % 2 == 0) Even += 1;
        }
    
        return  Even;
    }
    
    
    void DisplayArray (int A[], double Evens)
    {
    // Purpose: This function displays the elements of array A and the quantity of
    // Even values.
    
        cout << "Array elements: " << endl;
        for(int I = 0; I < MaxSize; ++I)
        {
            cout << "A[" << I << "] = " << A[I] << endl;
        }
    
        cout << "\n" << "Even Numbers: " << Evens << endl;
    }
    I've been working on it for several hours now... my problem I still have though is getting the GenerateRandomNumbers function to only generate N numbers... also, on the assignment when the program is calling on some of the functions the N is included in the parenthesis. whenever I try to do this, I get error messages.

  7. #7
    Unregistered
    Guest
    pass N to generateRandomNumbers, COuntEvenNumbers, and DisplayArray. Then replace each MaxSize within those functions with N. Change MaxSize back to local to main().

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    thanks... I went in and talk to my teacher last night, and he told me basically the same

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  3. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 09:14 AM
  4. Helllppp!!! I/O and array problems
    By dorky in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2005, 10:24 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM

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