Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Test score using pointers


  • Please log in to reply

#1
Ceaser

Ceaser

    New Member

  • Member
  • Pip
  • 1 posts
The question asks me to dynamically allocate an array large enough to hold a suer defined number of test scores, then pass to a function that sorts in ascending order. Another function should be called to average scores. and display the list and average. not supposes to accept negative test scores.

This is what i have created, it looks like everything is there. But when i try to make it run, it gives errors saying that it can't convert parameter one from double* to double*[] and others like can't convert double* to int* don't really now how to fix this problem. could someone please help in making sure i wrote it correctly and telling me how to repair the errors. Thank you.


#include <iostream>
#include <iomanip>
using namespace std;

void arrSelectSort(double *[], int);
void showArrPtr(double *[], int);
void showAverage(double , int);

int main()
{
double *scores, //To dynamically allocate an array
total=0, //Accumulator
average; //To hold the averge scores
int numScores; //To hold the number of test scores


//Get the number of test scores.
cout << "How many test scores will you be inputing?";
cin >> numScores;

//Dynamically allocate an array large enough to hold that many
//test scores
scores = new double[numScores];

//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
while (scores <=0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}

//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}

//sort the elements of the array pointers
arrSelectSort ( scores, numScores );

//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );

showAverage( total, numScores );

//Free memory.
delete [] scores;
scores = 0;

return 0;

}

void arrSelectSort(double *array[], int size)
{
int startScan, minIndex;
int *minElem;

for (startScan = 0; startScan < ( size - 1 ); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ( *(array[index] ) < *minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}

void showArrPtr(double *array, int size)
{
for (double count=0; count< size; count++)
cout << *(array[count]) << " ";
cout << endl;
}

void showAverage(double total, int numScores)
{
float average;

//Calculate the average
average = total / numScores;

//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
}


  • 0

Advertisements


#2
Diego8

Diego8

    Member

  • Member
  • PipPipPip
  • 189 posts
Sorry for this late reply.

In both errors you have a syntax error. double* and double*[] are different things. The first is a pointer to a double value. double*[] is an array where every element is a pointer to a double value.

"like can't convert double* to int*"

Well... here you have a big problem. double is grater than int so you can't convert a double value to an interger with an assigantion.
To do this you have to do a dynamic cast this is done using the dynamic_cast key word.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP