School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,098 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,823 people online right now. Registration is fast and FREE... Join Now!



Array

Array Array Rate Topic: -----

#1 Lucky79  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 7
  • Joined: 10-July 08


Dream Kudos: 0

Post icon  Posted 05 November 2008 - 01:29 PM

OK so I have written this program and I am stumped. I got it to display the average of the numbers, but I need it to output the ascending order and drop the lowest score. Then average without the lowest score. Any hints would be great.

Instructions:

1. Array Locator

Write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate. The function should return a pointer to the array.

2. Test Scores #1
Write a program that dynamically allocaates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.

Imput Validation: Do not accept negative numbers for test scores.

3. Drop Lowest Score
Modify Problem 2 above so the lowest test score is dropped. This score should not be included in the calculation of the average.



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

 //Function prototypes
 void arrSelectSort(int *[], int);
 void showArray(int [], int);
 void showArrPtr(int *[], int);

 int main()
 {
	 double *TestScores,  //To dynamically allocate an array
			total = 0.0,  //Accumulator
			    average; //To hold average test scores
	
	 int numTest, //To hold number of test scores
		   count; //Counter variable

	 //Get the number of test scores you wish to average and put in order
	 cout << "How many test scores do you wish ";
	 cout << "to enter? ";
	 cin >> numTest;

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

	 //Get the test scores
	 cout << "Enter the test scores below.\n";
	 for (count = 0; count < numTest; count++)
		 
	 {
		 cout << "Test Score " << (count + 1) << ": ";
		 cin >> TestScores[count];
	 }

	//Calculate the total test scores
    for (count = 0; count < numTest; count++)
	{
		total += TestScores[count];
	}

	//Calculate the average test scores
	 average = total / numTest;

	 //Dsiplay the results
	 cout << fixed << showpoint << setprecision(2);
	 cout << "The average of all the test score is " << average << endl;


	 //Free dynamically allocated memory
	 delete [] TestScores;
	 TestScores = 0; //make TestScores point to null


	 //An array of pointers to int
	 int *arrPtrTestScores[count];

	 //Each element of arrPtr is a pointer to int. Make each
	 //element point to an element in the donations array
	 for (int count = 0; count < TestScores[count]; count++)
		 arrPtr[count] = &TestScores[count];

	 //Sort the elements of the array of pointers
	 arrSelectSort(arrPtr, TestScores[count];

	//Display the Test Scores in ascending order
	 cout << "The test scores, sorted in ascending order, are: \n";
	 showArrPtr(arrPtr, TestScores[count]);
	 return 0;
 }

   //This function performs an ascending order selection sort
 void arrSelectSort(int *arr[], int size)
 {
	 int startScan, 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;
	 }
 }
 system("pause");
 return 0;
 }

MOD EDIT: Please :code:
Thanks, gabehabe :)
Was This Post Helpful? 0
  • +
  • -


#2 gabehabe  Icon User is offline

  • Black Scatmaster
  • Icon
  • View blog
  • Group: Alumni
  • Posts: 9,048
  • Joined: 06-February 08


Dream Kudos: 3300

Expert In: Lots of things.

Posted 05 November 2008 - 03:47 PM

1) arrPtr hasn't been declared, I assume you mean TestScores?
2) You're using an array of doubles, while your functions accept arrays of ints
3) When passing an array, you only need * or [] ~ not both (unless it's multidimensional)
4) A few syntax errors~ Missing brackets, here and there
5) Lots of double/int mixups along the way... Stick to one datatype~!
6) Your void function returns an integer~ not allowed!
7) You have declared functions, and even used one, which haven't been defined yet.

I've fixed all those bugs, and commented your code. Read it closely, there were a lot of mistakes~!

I haven't tested it, but it compiles now.
#include <iostream>
 #include <iomanip>
 using namespace std;

 //Function prototypes
 void arrSelectSort(double *, int); // passing an array of doubles, not ints!
 //void showArray(double *, int);
 //void showArrPtr(double *, int);

 int main()
 {
	 double *TestScores,  //To dynamically allocate an array
			total = 0.0,  //Accumulator
			    average; //To hold average test scores

	 int numTest, //To hold number of test scores
		   count; //Counter variable

	 //Get the number of test scores you wish to average and put in order
	 cout << "How many test scores do you wish ";
	 cout << "to enter? ";
	 cin >> numTest;


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

	 //Get the test scores
	 cout << "Enter the test scores below.\n";
	 for (count = 0; count < numTest; count++)

	 {
		 cout << "Test Score " << (count + 1) << ": ";
		 cin >> TestScores[count];
	 }

	//Calculate the total test scores
    for (count = 0; count < numTest; count++)
	{
		total += TestScores[count];
	}

	//Calculate the average test scores
	 average = total / numTest;

	 //Dsiplay the results
	 cout << fixed << showpoint << setprecision(2);
	 cout << "The average of all the test score is " << average << endl;


	 //Free dynamically allocated memory
	 delete [] TestScores;
	 TestScores = 0; //make TestScores point to null


	 //An array of pointers to int
	 int *arrPtrTestScores[count];

	 //Sort the elements of the array of pointers
	 arrSelectSort(TestScores, TestScores[count]);

	//Display the Test Scores in ascending order
	 cout << "The test scores, sorted in ascending order, are: \n";
	 // showArrPtr hasn't been defined yet~!
	 //showArrPtr(TestScores, TestScores[count]);
	 return 0;
 }

   //This function performs an ascending order selection sort
 void arrSelectSort(double *arr, int size)
 {
	 int startScan;
	 double minIndex; // one datatype
	 double minElem; // has to be the same datatype

	 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;
			 }
		 }
		 // because minIndex is a double, and array subscripts require ints, we have
		 // to cast it~ (int)variable will cast it to an int
		 arr[(int)minIndex] = arr[startScan];
		 arr[startScan] = minElem;
	 }
 } // one too many of these~!

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month