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:
  #1  
Old November 17th, 2006, 07:46 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
Need help with dynamically allocating an array

i need to figure out how to write a program that dynamically allocates an array large enough to hold any number of test scores the user wishes to enter using the function i wrote in the earlier submission. after the score are enter, the array should be passed to a function that sorts theem in ascending order and another fucntion should then be called to calculate the average score. can anyone help?
Thanks in advance.

Reply With Quote
  #2  
Old November 17th, 2006, 08:35 AM
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
Hi joelw

Here is how you get a dynamically allocated array:

Code:
#include <iostream>

int main()
{
	std::cout << "How many elements do you need? : ";
	int elems;
	std::cin >> elems;

	double *myArray = new double [elems]; <-- Like this
	
	for ( int i = 0; i < elems; i++ )
	{
		std::cout << "Enter number: ";
		std::cin >> myArray[i];
	}

	for ( int i = 0; i < elems; i++ )
	{
		std::cout << myArray[i] << ' ';
	}
	std::cout << std::endl;

	delete [] myArray;  // DO NOT FORGET THIS

	return 0;
}


And here is a link to some sorting algorithms. Take your pick:

Sorting

Nice tidy code by the way In your other post.

Reply With Quote
  #3  
Old November 17th, 2006, 02:51 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 so in this code example would you think i would ask "How many Test Scores do you need?" and i also would have to write a sort in ascending order right?

Reply With Quote
  #4  
Old November 17th, 2006, 02:59 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
You can ask whatever you like. The only part you really need is this:

Code:
std::cout << "How many elements do you need? : ";
int elems;
std::cin >> elems;

double *myArray = new double [elems]; <-- Like this


I did did the rest as an example. Yes you need an algorithm to sort the array. If you want you could also do this:

Code:
#include <algorithm>

sort( array, array + size );


Just depends if you are doing this as an assignment and are allowed to use it. If you need any more help, pop back.

Reply With Quote
  #5  
Old November 18th, 2006, 04:25 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
the first code you sent does that work because tried to see what it would look like and it gave me debugging errors?

Reply With Quote
  #6  
Old November 18th, 2006, 04:54 AM
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
Yes it works. What debugging errors? Works fine on visual studio and codeblocks. Memory gets deleted as it should.

Reply With Quote
  #7  
Old November 18th, 2006, 06:06 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
ok this is what ive got so far but its giving me error of:
overloaded function with no contextual type information
do you know what that means?




Code:
int main()
{
    double *scores, total = 0, average;
    int numAmount;
    
    cout << "How many test scores are you going to record? ";
    cin >> numAmount;
    scores = new double[numAmount];
    
    cout << "Please enter in your test scores below.\n ";
    for (count = 0; count < numAmount; count++)
        {
               cout << "Test Score " << (count + 1) << ": ";
               cin >> scores[count];
        }

Reply With Quote
  #8  
Old November 18th, 2006, 07:27 AM
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
Are you sure that is all you have done? That bit there what you have posted works fine for me, apart from putting in the appropriate header. The contextual information thing is where there are naming conlicts and the compiler cannot distinguish between each one. Check your functions calls and return parameters.

Reply With Quote
  #9  
Old November 18th, 2006, 08:16 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
so you think my heading should be different? so you compiled it and there was no errors? it must the cheap free compiler i am using at work. Now, looking at the assignment instructions from the previous post would that be the route you would go?

Reply With Quote
  #10  
Old November 18th, 2006, 08:30 AM
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
All i did was this:

Code:
#include <iostream>

int main()
{
    double *scores, total = 0;  // average;
    int count;  // added this
    int numAmount;
    
	std::cout << "How many test scores are you going to record? ";
	std::cin >> numAmount;
        scores = new double[numAmount];
    
	std::cout << "Please enter in your test scores below.\n";
    for ( count = 0; count < numAmount; count++ )
    {
        std::cout << "Test Score " << (count + 1) << ": ";
        std::cin >> scores[count];
    }
	
	delete [] scores;

	return 0;
}


And that works fine. What compiler are you using by the way?

Reply With Quote
  #11  
Old November 18th, 2006, 09:00 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
im using a compiler call DEV-C++ ever heard of it?

Reply With Quote
  #12  
Old November 18th, 2006, 09:09 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
ok that worked and that pretty much what the assignment is asking to do but it also wants it sorted in ascending order. so basically i would just do a sort array like for my function prototype i would do something like
void sortArray(double (&array)[], int size);
void showArray(double array[], int size);
right?

Reply With Quote
  #13  
Old November 18th, 2006, 09:14 AM
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
Yes you would, i just compiled it on Dev-cpp and it worked also.

Reply With Quote
  #14  
Old November 18th, 2006, 09:43 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
no when i sort it is there a way to bring back the results going down the screen because on my previous assignment i did it went like this: test 1 test 2 test 3 ect. and i want it to look like this: test 1
test 2
test 3 ect?
i would also leave my sort[]brackets empy because there is a specific amount test to sort right?

Reply With Quote
  #15  
Old November 18th, 2006, 09:55 AM
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
To get it to output down the screen just put:

Code:
cout << scores[count] << endl;


Why don't you just pass the address of the array instead of a reference. The address deals directly with the array anyway.

Reply With Quote
  #16  
Old November 18th, 2006, 10:00 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
can you give me an example so that i can better understand it

Reply With Quote
  #17  
Old November 18th, 2006, 10:16 AM
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
Like this, i have put comments in there for you to look at.

Code:
#include <iostream>

// just pass the address of the array - see below
void sortArray( double arr[], int sz );
// instead of a reference - see below, you will run
// into all sorts of problems with it being dynamic
// void sortArray( double ( &arr[] ), int sz );
void displayArray( double arr[], int sz );


int main()
{
    double *scores, total = 0;  // average;
    int count;
    int numAmount;
    
	std::cout << "How many test scores are you going to record? ";
	std::cin >> numAmount;
    scores = new double[numAmount];
    
	std::cout << "Please enter in your test scores below.\n";
    for ( count = 0; count < numAmount; count++ )
    {
        std::cout << "Test Score " << (count + 1) << ": ";
        std::cin >> scores[count];
    }
	
	// pass the address and the size of the array
	// scores is a pointer for the address of scores[0]
	sortArray( scores, numAmount );
	displayArray( scores, numAmount );
	
	delete [] scores;

	return 0;
}

void sortArray( double arr[], int sz )
{
	// your sorting algorithm here....
}

void displayArray( double arr[], int sz )
{
	std::cout << std::endl;  // for spacing
	for ( int i = 0; i < sz; i++ )
	{
		std::cout << "Score " << ( i + 1 ) << " : ";
		// prints out a score and then moves down to
		// the next line so you have
		// score 1 : 20.2
		// score 2 : 10.2 etc...
		std::cout << arr[i] << std::endl;
	}
}

Reply With Quote
  #18  
Old November 18th, 2006, 11:34 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
ok im starting to understand this now when i compiled what you gave me to see the results it show test1 vertical like i wanted now is there a way to have the actual test scores ascend instead?

Reply With Quote
  #19  
Old November 18th, 2006, 11:47 AM
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
You mean like this?:

Code:
void displayArray( double arr[], int sz )
{
	std::cout << std::endl;  // for spacing
        // start at the back of the array until 0
	for ( int i = sz - 1; i >= 0; i-- )
	{
		std::cout << "Score " << ( i + 1 ) << " : ";
		// prints out a score and then moves down to
		// the next line so you have
		// score 1 : 20.2
		// score 2 : 10.2 etc...
		std::cout << arr[i] << std::endl;
	}
}


Printing the array last number first.

Reply With Quote
  #20  
Old November 18th, 2006, 11:55 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
that looks like descending order what i meant is can we sort the scores in ascending order instead for example
test 2: 1
test 1: 2
test 3: 3

you see what i mean.
also this is a dynamically allocated program right?

Reply With Quote
  #21  
Old November 18th, 2006, 12:10 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
I thought you were sorting the array. You mean you want the score number associating with the score amount? Yes, it is a dynamically allocated array. The size of the array is not known until the program is run.

Reply With Quote
  #22  
Old November 18th, 2006, 12:16 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
yes i want the score number associating with the score amount is that possible and secondly i am suppose to also ad a averaging function in there and this is what is giving me a problem did do something wrong?

Code:
void displayAverage(double arr[], int sz)
{
     cout << "The average all test scores is " << siz << endl;
     average = scores / numAmount;
     
}     

Reply With Quote
  #23  
Old November 18th, 2006, 12:24 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
To get the average, you need to sum all the numbers in the array with a loop and then divide by the sum.

Like this:

Code:
void displayAverage(double arr[], int sz)
{
    double sum = 0.0;

    for ( int i = 0; i < sz; i++ )
    {
         sum += arr[i];
    }

    double average = sum / sz;
    cout << "The average all test scores is " << average << endl;     
}

Reply With Quote
  #24  
Old November 18th, 2006, 12:32 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
i got you i keep on forgetting the loops with the function.
Now if i were to write so the lowest test score was dropped how would i do that? and also about the ascending the way you did is the only way can we not do it the way i was saying?

Reply With Quote
  #25  
Old November 18th, 2006, 01:09 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
Yes it is possible, you just need another container to run alongside your scores. When you sort the scores, sort the other container the same way.To find the lowest score search for the lowest and then print out all the ones that are above the lowest.

When you sort the other container you just need to sort the position to match the scores.

Last edited by Paul820 : November 18th, 2006 at 01:14 PM.

Reply With Quote
  #26  
Old November 18th, 2006, 01:14 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 so should i also create another fucntion to do it?

Reply With Quote
  #27  
Old November 18th, 2006, 02:34 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, this works. You now need to put that into your program:

Code:
#include <iostream>

int main()
{
	std::cout << "Please enter the amount of scores to catalogue: ";
	int amount;
	std::cin >> amount;

	double *scores = new double[amount];
	int *scorePos = new int[amount];  // the scores position

	for ( int i = 0; i < amount; i++ )
	{	
             std::cout << "Enter score No. " << ( i + 1 ) << " : ";
	     std::cin >> scores[i];
	     scorePos[i] = i + 1;  // make the scorePos have the correct value, otherwise the first one would be zero
	}

        double temp;
        int posTemp;  // this is to hold the score position
	                   // prior to sorting the arrays
	
	// sort them both so they are in line with each other
        for ( int i = 0; i < amount - 1; i++ )
        {
	    for ( int j = ( i + 1 ); j < amount; j++ )
            {
                if ( scores[i] < scores[j] )
                {
		    temp = scores[i];
		    posTemp = scorePos[i];
                    scores[i] = scores[j];
		    scorePos[i] = scorePos[j];
                    scores[j] = temp;
		   scorePos[j] = posTemp;
                }
            }
       }

       std::cout << std::endl;

	// print them all out
	for ( int  i = 0; i < amount; i++ )
	{
	    std::cout << "Score No. " << scorePos[i];
            std::cout << " : " << scores[i] << std::endl;
	}
         
         delete [] scorePos;
         delete [] scores;

	return 0;
}


Edit: changed int temp; to double temp;

Last edited by Paul820 : November 18th, 2006 at 03:10 PM.

Reply With Quote
  #28  
Old November 19th, 2006, 07: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
ok i put that code in visual c++ and it gave me an error

'i' : redefinition; multiple initialization

What does this mean?

Reply With Quote
  #29  
Old November 19th, 2006, 09:03 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
You are using the variable 'i' more than once in the same scope. Just change one of the variables that you have called i to a different letter or give it a meaningful name.

Reply With Quote
  #30  
Old November 21st, 2006, 04:38 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
ok that worked and now the test scores are in decsending order can we fip flop that and make the test scores in acsending order? also do i just put my sort functions around this program?

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