Thread: new to c++

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    new to c++

    so i'm new to c++ and tying to write a program that will hold test scores then they have to be arrange in ascending order and average them all tighter at then end. I got this far and keep getting an error of cannot convert parameter 1 from int to int* for "showAverage". I am not sure what is wrong please help!

    Code:
    //This program will hold a user-defined number of test scores
    //Then it will sort them in ascending order and average them.
    #include <iostream>
    using namespace std;
    
    
    //function prototype
    void arrSelectSort (int *, int);
    void showAverage (int *, int);
    void showArrPtr (int *, int);
    
    
    int main()
    {
        int *scores,                //to dynamically allocate an array
            average,                //to hold the average test score
            total=0,                //accumulator
             numTest,                //to hold the number of test scores
            count;                    //counter variable
    
    
        //get the number of test scores.
        cout <<"Test Scores" << endl;
        cout << "How many test scores do you need to average? ";
        cin >> numTest;
    
    
        //dinamically allocate an array large enough to hold that many test scores.
        scores=new int [numTest];
    
    
        //get the test score
        cout <<"Enter the test scores to be average: " <<endl;
        for (count=0; count<numTest; count++)
        {
            cout<<"Test " << (count+1) << " : ";
            cin >> scores [count];
            //do not accept negative test scores numbers
            while (scores <0)
            {
                cout <<"Enter a number greater than 0";
                cin >> scores [count];
            }
        }
        //calculate the total scores together
        for (count=0; count<numTest; count++)
        {
            total+=scores[count];
        }
    
    
        //sort the elements of the array of pointers
        arrSelectSort(scores, scores[count]);
    
    
        //display the test scores in ascending order
        cout<<"The test scores in ascending order are: " << endl;
        showArrPtr (scores,scores[count]);
    
    
        //display the average test score
        showAverage (total, numTest);
    
    
        //free allocated memory
        delete [] scores;
        scores=0;                    //make scores point to null.
    
    
        return 0;
    }
    //******************************************************
    //definition of function showAverage
    //this funtion displays the average number of test scores
    //***************************************************
    
    
    void showAverage (int total, int numTest)
    {
        int average;
        //calculate the average
        average=total/numTest;
        //display the results.
        cout<<"Average Score: "<<average<<endl;
    }
    
    
    
    
    //**************************************************
    //definition of function arrSelectSort
    //this funtion will perform the ascending order selection sort
    //*****************************************************
    
    
    void arrSelectSort (int *arr[], int size)
    {
        int startScan;
        int minIndex;
        int *minElem;
    
    
        for (startScan=0; startScan<(size-1); startScan++)
        {
            minIndex=startScan;
            minElem=arr[startScan];
            for (int index= startScan + 1; index <size; index++)
            {
                if (*(arr[index])<*minElem)
                {
                    minElem=arr[index];
                    minIndex=index;
                }
            }
            arr[minIndex] = arr[startScan];
            arr[startScan] = minElem;
        }
    }
    //*******************************************************
    //definition of function showArrayPtr
    //this function displays the contents of the array pointed to by arr
    //*********************************************************
    
    
    void showArrPtr (int *arr[], int size)
    {
        for (int count=0; count <size; count++)
            cout<<*(arr[count])<<" ";
            cout<<endl;
    }
    Last edited by Cindy Reyes; 04-13-2014 at 06:14 PM.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    First thing's first, learn how to use code tags.

    You need to use "[ code ] " and "[ /code ]" but without the spaces.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void showAverage (int *, int);
    vs
    > void showAverage (int total, int numTest)

    Tip: Write the empty function first, then copy/paste it to make the prototype.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make scores a std::vector instead. See std::vector - cppreference.com.
    Your functions are accepting an array of pointers. Your score in not an array of pointers. It's a dynamic array. You need to fix that.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    //This program will hold a user-defined number of test scores
    //Then it will sort them in ascending order and average them.
    #include <iostream>
    #include <vector>
    
    //get out of the habbit of "using namespace std;"
    using std::cin;
    using std::cout;
    using std::endl;
    
    
    
    
     
    void arrSelectSort (int *, int);
    double showAverage (std::vector<double>&);
    void showArrPtr (std::vector<double>&);
     
    
    
    int main()
    {
        double grade,
        numTest,                //to hold the number of test scores
        count;                    //counter variable
        std::vector<double>scores;
        
        //get the number of test scores.
        cout <<"Test Scores" << endl;
        cout << "How many test scores do you need to average? ";
        cin >> numTest;
    
        //get the test score
        cout <<"Enter the test scores to be average: " <<endl;
        while (count <= numTest)
        {
            cin>>grade;
            if (grade>=101)
            {
                cout<<"Come on grades cannot be greater then 100%\n";
                cin.ignore();
            }
            scores.push_back(grade);
            count++;
        }
       
        //sorting the vector
        std::sort(scores.begin(), scores.end());
        
        //display the test scores in ascending order
        cout<<"The test scores in ascending order are: " << endl;
        showArrPtr (scores);
        
        //display the average test score
        cout<<"The average is: "<<showAverage (scores);
        
        return 0;
    }
    //******************************************************
    //definition of function showAverage
    //this funtion displays the average number of test scores
    //***************************************************
    
    
    double showAverage (std::vector<double>&v)
    {
        double average=0;
        int count =0;
        for (auto it=v.begin() ; it!=v.end(); ++it)
        {
            average += *it;
            count++;
        }
        average = average / count;
        
        return average;
    }
    
    
    //*******************************************************
    //definition of function showArrayPtr
    //this function displays the contents of the array pointed to by arr
    //*********************************************************
    
    
    void showArrPtr (std::vector<double>& v)
    {
        for (auto it=v.begin(); it!=v.end(); ++it)
        {
            cout<<*it<<"\n";
        }
    }


    Consider something like this... Look more into vectors and passing by reference.
    Last edited by jocdrew21; 04-14-2014 at 07:09 AM.

Popular pages Recent additions subscribe to a feed