C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #61  
Old November 21st, 2006, 02:21 PM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 12
ok i got it working now back to the sorting

should i use functions like this

void sortArray( double arr[], int sz, int scoresPos[], int size );
void displayArray( double arr[], int sz, int scoresPos[], int size );

Reply With Quote
  #62  
Old November 21st, 2006, 02:42 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Yeah, just like your last project. You are doing exactly the same thing. Only this time it isn't dynamic arrays.

Reply With Quote
  #63  
Old November 21st, 2006, 03:43 PM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 12
now for sortArray(scores,numAmount,scorePos,numAmount);
what would i put?

Reply With Quote
  #64  
Old November 21st, 2006, 03:57 PM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 12
ok seriously what am i doing wrong?

Code:
#include <iostream>
#include <iomanip>

using namespace std;

// Function prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortArray(double[], int sz, int size);
void displayArray(double[], int sz, int size);

int main()
{
    const int MONTHS = 12;
    double amount[MONTHS],           // To hold the amount of rainfall
           total,                    // To hold the total rainfall
           average,                  // To hold the average rainfall
           highest,                  // To hold the highest rainfall amount
           lowest;                   // To hold the lowest rainfall amount
    
    cout << "Enter the rainfall for each month.\n";
    for (int count = 0; count < MONTHS; count++)
    {
        cout << "Month " << (count + 1) << ": ";
        cin >> amount[count];
    }
    
	

    // Get the total monthly rainfall
    total = sumArray(amount, MONTHS);
    
    // Calculate the average rainfall
    average = total / MONTHS;
    
    // Find the highest monthly rainfall
    highest = getHighest(amount, MONTHS);
    
    // Find the lowest monthly rainfall
    lowest = getLowest(amount, MONTHS);
    
    // Display the results
    cout << fixed << showpoint << setprecision(2);
    cout << "The total monthly rainfall is "  << total << " inches" << endl;
    cout << "The average monthly rainfall is "  << average << " inches" << endl;
    cout << "The highest monthly rainfall is "  << highest << " inches" << endl;
    cout << "The lowest monthly rainfall is "  << lowest << " inches " << endl;          
    
	return 0;
}



//**************************************************  ****************
// Definition of sumArray                                          *
// This function accepts a double array and its size               *
// as arguments. The sum of the array's elements                   *
// is returned as an double.                                       *
//**************************************************  ****************

double sumArray(double array[], int size)
{
       double total = 0;
       
       for (int count = 0; count < size; count++)
           total += array[count];
       return total;
}

//**************************************************  ****************
// Definition of getHighest                                        *
// This function accepts a double array and its size               *
// as arguments. The highest value in the array is                 *
// returned as an double.                                          *
//**************************************************  ****************

double getHighest(double array[], int size)
{
       double highest;
       
       highest = array[0];
       for (int count = 1; count < size; count++)
       {
           if (array[count] > highest)
              highest = array[count];
       }
       return highest;
}

//**************************************************  *****************
// Definition of getLowest                                          *
// This function accepts a double array and its size                *
// as arguments. The lowest value in the array is                   *
// returned as an double                                            *
//**************************************************  *****************

double getLowest(double array[], int size)
{
       double lowest;
       
       lowest = array[0];
       for (int count = 1; count < size; count++)
       {
           if (array[count] < lowest)
              lowest = array[count];
       }
       return lowest;
}

//**************************************************  *****************
// Definition of sortArray                                          *
// This function excepts a double array and its size as an          *
// argument. The results will be display from highest to lowest     *
//**************************************************  *****************

void sortArray(double[], int sz, int size)
{
	for ( int i = 0; i < amount - 1; i++ )
{
     for ( int j = ( i + 1 ); j < amount; j++ )
     {
           if ( MONTHS[i] < MONTHS[j] )
           {
                temp = MONTHS[i];
	        posTemp = amount[i];
                MONTHS[i] = MONTHS[j];
		amount[i] = amount[j];
                MONTHS[j] = temp;
	        amount[j] = posTemp;
            }
      }
}

Reply With Quote
  #65  
Old November 21st, 2006, 06:22 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Here you go, look through that. It works.

Code:
#include <iostream>
#include <iomanip>

using namespace std;

// Function prototypes
double sumArray(double array[], int size);
double getHighest(double array[], int size);
double getLowest(double array[], int size);
void sortArray(double array[], int size, int amntPos[], int sz ); // send array by reference
void showArray(double array[], int size, int amntPos[], int sz );

int main()
{
    const int MONTHS = 12;
    int values[MONTHS]      = {12,11,10,9,8,7,6, 5, 4, 3, 2,1 };
    double amount[MONTHS];
	int amountPos[MONTHS];  // The rainfall amounts position

    double total;      // To hold the total rainfall
    double average;    // To hold the average rainfall
    double highest;    // To hold the highest rainfall amount
    double lowest;     // To hold the lowest rainfall amount
    
    cout << "Enter the rainfall for each month.\n";
    for ( int count = 0; count < MONTHS; count++ )
    {
        cout << "Month " << ( count + 1 ) << ": ";
        cin >> amount[count];
	amountPos[count] = count + 1;  // just like last time,
	// you want your first value to be number one. That is the
	// position of your first value entered
    }
    
    // Get the total monthly rainfall
    total = sumArray(amount, MONTHS);
    
    // Calculate the average rainfall
    average = total / MONTHS;
    
    // Find the highest monthly rainfall
    highest = getHighest(amount, MONTHS);
    
    // Find the lowest monthly rainfall
    lowest = getLowest(amount, MONTHS);
    
    // Display the results
    cout << fixed << showpoint << setprecision(2);
    cout << "The total monthly rainfall is "  << total << " inches" << endl;
    cout << "The average monthly rainfall is "  << average << " inches" << endl;
    cout << "The highest monthly rainfall is "  << highest << " inches" << endl;
    cout << "The lowest monthly rainfall is "  << lowest << " inches " << endl;          
    cout << "The monthly rainfall in descending order:\n";
	// pass the amount position aswell, as that needs sorting
    sortArray( amount, MONTHS, amountPos, MONTHS );
	// same here, pass the other array.
    showArray(amount, MONTHS, amountPos, MONTHS );
    // system("PAUSE"); use cin.get(); this isn't standard
    return EXIT_SUCCESS;

    delete [] amount;
    delete [] amountPos;
}
 
//**************************************************    ****************
// Definition of sumArray                                          *
// This function accepts a double array and its size               *
// as arguments. The sum of the array's elements                   *
// is returned as an double.                                       *
//**************************************************    ****************
double sumArray(double array[], int size)
{
    double total = 0;
    for (int count = 0; count < size; count++)
    total += array[count];
    return total;
}

//**************************************************    ****************
// Definition of getHighest                                        *
// This function accepts a double array and its size               *
// as arguments. The highest value in the array is                 *
// returned as an double.                                          *
//**************************************************    ****************
double getHighest(double array[], int size)
{
    double highest;
       
    highest = array[0];
    for (int count = 1; count < size; count++)
    {
        if (array[count] > highest)
        highest = array[count];
    }
    return highest;
}
//**************************************************    *****************
// Definition of getLowest                                          *
// This function accepts a double array and its size                *
// as arguments. The lowest value in the array is                   *
// returned as an double                                            *
//**************************************************    *****************
double getLowest(double array[], int size)
{
    double lowest;
       
    lowest = array[0];
    for (int count = 1; count < size; count++)
    {
        if (array[count] < lowest)
        lowest = array[count];
    }
    return lowest;
}
 
//**************************************************    *****************
// Definition of function sortArray                                 *
// This function performs an descending order selection sort on     *
// array. elems is the number of elements in the array.             *
//**************************************************    *****************
// You were forgetting to change the names. These names are different
// from your last project. 
void sortArray(double array[], int size, int amntPos[], int sz )
{
    int posTemp;
	double temp = 0;

    for ( int i = 0; i < size - 1; i++ )
    {
	for ( int j = ( i + 1 ); j < sz; j++ )
	{
             if ( array[i] < array[j] )
             {
                temp = array[i];
		posTemp = amntPos[i];
                array[i] = array[j];
	        amntPos[i] = amntPos[j];
                array[j] = temp;
		amntPos[j] = posTemp;
           }
        }
    }
}
 

//**************************************************    ******************
// Definition of function showArray.                                 *
// This function displays the contents of array. elems is the        *
// number of elements.                                               *
//**************************************************    ******************
void showArray(double array[], int size, int amntPos[], int sz )
{
    for ( int count = 0; count < size; count++ )
	{
	    cout << "Month: " << setw( 4 ) << amntPos[count] << "  "; 
           cout << setw( 6 ) << array[count] << " ";
	   cout << endl;
	}
}

Last edited by Paul820 : November 21st, 2006 at 06:25 PM.

Reply With Quote
  #66  
Old December 14th, 2006, 09:05 AM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 12
hello,
ok this code works fine but it still dosnt compute the avearage with the lowest score dropped.
here is my code.
Code:
#include <iostream>
#include <iomanip>

using namespace std;

void sortArray( double arr[], int sz, int scoresPos[], int size );
void displayArray( double arr[], int sz, int scoresPos[], int size );


int main()
{
    double *scores, total = 0;  // average;
	
    int count;
    int numAmount;
    
    cout << "How many test scores are you going to record? ";
    cin >> numAmount;
    scores = new double[numAmount];
    int *scorePos = new int[numAmount];

    cout << "Please enter in your test scores below.\n";
    for ( count = 0; count < numAmount; count++ )
    {
        cout << "Test Score " << (count + 1) << ": ";
        cin >> scores[count];
	scorePos[count] = count + 1;
    }
	

	sortArray( scores, numAmount, scorePos, numAmount );
	displayArray( scores, numAmount, scorePos, numAmount );
	
	delete [] scores;
	delete [] scorePos;


    return 0;
}

//**************************************************  *********************
// Definition of a sortArray                                            *
// This function excepts a double array and its size as an              *             
// argument. The results will be diplayed in ascending order            *
//**************************************************  *********************


void sortArray( double arr[], int sz, int scoresPos[], int size )
{
    double temp = 0;
    int posTemp = 0;

    for ( int i = 0; i < sz - 1; i++ )
    {
        for ( int j = ( i + 1 ); j < sz; j++ )
	{
            if ( arr[i] > arr[j] )
           {
                temp = arr[i];
		posTemp = scoresPos[i];
                arr[i] = arr[j];
		scoresPos[i] = scoresPos[j];
                arr[j] = temp;
		scoresPos[j] = posTemp;
           }
        }
     }
}

//**************************************************  ***********************
// Definition of a display array                                          *
// This function excepts array and its size as an argument. The           *
// average will be displayed dropping the lowest score.                   *
//**************************************************  ***********************


void displayArray( double arr[], int sz, int scoresPos[], int size )
{
	double average = 0;
	double sum = 0;

	std::cout << std::endl;  // for spacing
	for ( int i = 0; i < sz; i++ )
	{
		cout << "Score " << scoresPos[i] << " : ";
		cout << arr[i] << std::endl;
	}
	
	int j;
	for ( j = 1; j < sz; j++ )
	{
		sum += arr[j];
	}

	average = sum / j;

	cout << "\nThe average score disregarding the lowest score is: " << average << endl;
    cout << fixed << showpoint << setprecision(2);
    
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need help with dynamically allocating an array


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap