Need Help (Array)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 1
Reputation: Supercharger is an unknown quantity at this point 
Solved Threads: 0
Supercharger Supercharger is offline Offline
Newbie Poster

Need Help (Array)

 
0
  #1
Sep 16th, 2009
i need help with this C++ array excercise.... it seem quite simple but im not sure what code i need to put where. help is greatly appreciated...


Excerise::
http://www.lhs.logan.k12.ut.us/~rweeks/cp1/array.htm
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Need Help (Array)

 
1
  #2
Sep 16th, 2009
FIrstly Please Donot Send PM'S asking for people to solve your problem. There are high chances that many will just Ignore your problem and you will not even get suggestions to your problem.
The proof of your PM
Originally Posted by Supercharger
Sorry for the random message. But would you care to help me solve this C++ ?

http://www.lhs.logan.k12.ut.us/~rweeks/cp1/array.htm

Thank you so much if you do.

Secondly :
You are not going to get the solution of your problem If you show us nothing but a simple link to the assignment. This forum is a discussion group which is ready to provide help, if the effort is shown. THATS THE RULE HERE !!

So this could go into 2 ways,
Either you spend some time read through the books and then try to write the code yourself and then when you have a problem post it onto the forum with CODE so that we can help you out at that time and every1's happy .

Or you spend the rest of the time hanging around forums asking everyone in there to help you out . ( I'm pretty sure that mostly '0' would help at this forum . ) And as you have already posted the assignment here. Many of the members will catch your link on the other forums as-well, and leave your ASSIGNMENT incomplete.

The Decision is yours.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Need Help (Array)

 
0
  #3
Sep 16th, 2009
//check out the code snippet, here
int random(int low, int high) {
return
}
  1. //use logic to find the min element
  2. //assign initial min element to A[0];
  3. //search through loop seeing if other element's value are
  4. //greater than min element value if so the reassign min element
  5. //other wise keep searching
  6. int getMin(int A[], int max) {
  7. ...
  8. }
  9. //same as min, but use ">" when comparing
  10. int getMax(int A[], int max) {
  11. ...
  12. }
  13. //create a temp variable and assign it to first
  14. //assign second to first
  15. //assign temp to first
  16. void swap(int &first, int &second) {
  17. ...
  18. }
Last edited by firstPerson; Sep 16th, 2009 at 5:22 pm.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

Re: Need Help (Array)

 
-1
  #4
Sep 16th, 2009
Here is what I came up with.

"mystuff.h"
  1. #ifndef MYSTUFF_H
  2. #define MYSTUFF_H
  3.  
  4. void randomize();
  5.  
  6. int random( int high, int low = 0);
  7.  
  8. int getMax( int A[] );
  9.  
  10. int getMin( int A[] );
  11.  
  12. int swap( int A[] );
  13.  
  14. #endif

"mystuff.cpp"
  1. #include <time.h>
  2. #include <stdlib.h>
  3.  
  4. #include "mystuff.h"
  5.  
  6. void randomize()
  7. {
  8. srand(time(NULL));
  9. }
  10.  
  11. int random( int high, int low )
  12. {
  13. return rand() % (high-low) + low;
  14. }
  15.  
  16. int getMax( int A[] )
  17. {
  18. int max = A[0];
  19. for( int i = 1; i < 10; i++ )
  20. {
  21. if( A[i] > max )
  22. {
  23. max = A[i];
  24. }
  25. }
  26. return max;
  27. }
  28.  
  29. int getMin( int A[] )
  30. {
  31. int min = A[0];
  32. for( int i = 1; i < 10; i++ )
  33. {
  34. if( A[i] < min )
  35. {
  36. min = A[i];
  37. }
  38. }
  39. return min;
  40. }
  41.  
  42. int swap( int A[] )
  43. {
  44. for( int i = 0; i < 10; i++ )
  45. {
  46. int temp = A[i];
  47. int ran = random(10);
  48. A[i] = A[ran];
  49. A[ran] = temp;
  50. }
  51. return A[10];
  52. }

"array.cpp"
  1. #include <iostream>
  2.  
  3. #include "mystuff.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. //declare variables and set random seed
  10. int A[10], min, max;
  11. randomize();
  12.  
  13. //make random numbers and assign to array
  14. for(int i = 0; i < 10; i++)
  15. {
  16. A[i] = random(100, -100);
  17. }
  18.  
  19. //find min and max numbers in array
  20. min = getMin( A );
  21. max = getMax( A );
  22.  
  23. //output array before shuffled
  24.  
  25. for( int i = 0; i < 10; i++ )
  26. {
  27. if( i != 9 )
  28. {
  29. cout << A[i] << " ";
  30. }
  31. else
  32. {
  33. cout << A[i] << endl;
  34. }
  35. }
  36.  
  37. //output min and max values
  38. cout << "Low: " << min << "\nHigh: " << max << endl;
  39.  
  40. //shuffle the array
  41. swap( A );
  42.  
  43. //output shuffled array
  44. cout << "Shuffled: ";
  45. for( int i = 0; i < 10; i++ )
  46. {
  47. if( i != 9 )
  48. {
  49. cout << A[i] << " ";
  50. }
  51. else
  52. {
  53. cout << A[i] << endl;
  54. }
  55. }
  56.  
  57. system("PAUSE");
  58. return 0;
  59. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 73
Reputation: dgr231 is on a distinguished road 
Solved Threads: 7
dgr231's Avatar
dgr231 dgr231 is offline Offline
Junior Poster in Training

Re: Need Help (Array)

 
0
  #5
Sep 17th, 2009
Hey,

I'm still pretty new to C++, but there are a few comments that I would make regarding mystuff.cpp:

1. You will want to inlude the <ctime> header as that contains the time function you use for seeding your random number generator.

2. It may serve you better for your swap function to pass a pointer to the first index of the array as your arguement so you can directly alter the array inside the function and set the return type to void:
  1. void swap(int* firstIndex)

-D
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

Re: Need Help (Array)

 
0
  #6
Sep 17th, 2009
I am using <time.h> for the srand() function and <stdlib.h> for the time variable.

As for the swap function I knew the way I did it was bad but I am not too great at passing stuff by pointers. I self taught myself C++ and when reading about pointers and references I didn't fully understand =(.

Here is my change to that.
  1. void *swap( int A[] )
  2. {
  3. for( int i = 0; i < 10; i++ )
  4. {
  5. int temp = A[i];
  6. int ran = random(10);
  7. A[i] = A[ran];
  8. A[ran] = temp;
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Need Help (Array)

 
0
  #7
Sep 17th, 2009
arrays are pass by reference. They are pointers-to-ints. When you
pass an array to a function, and change its value you are changing the
original content and not its copy.

so this code :
  1. int swap( int A[] )
  2. {
  3. for( int i = 0; i < 10; i++ )
  4. {
  5. int temp = A[i];
  6. int ran = random(10);
  7. A[i] = A[ran];
  8. A[ran] = temp;
  9. }
  10. return A[10];
  11. }
You are changing the array original content. What you are returning is
junk since your array ranges from 0-9. And even if it wasn't junk you
would not be returning the whole array, just 1 element.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Need Help (Array)

 
0
  #8
Sep 17th, 2009
arrays are pass by reference. They are pointers-to-ints. When you
pass an array to a function, and change its value you are changing the
original content and not its copy.

so this code :
  1. int swap( int A[] )
  2. {
  3. for( int i = 0; i < 10; i++ )
  4. {
  5. int temp = A[i];
  6. int ran = random(10);
  7. A[i] = A[ran];
  8. A[ran] = temp;
  9. }
  10. return A[10];
  11. }
You are changing the array original content. What you are returning is
junk since your array ranges from 0-9. And even if it wasn't junk you
would not be returning the whole array, just 1 element.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 73
Reputation: dgr231 is on a distinguished road 
Solved Threads: 7
dgr231's Avatar
dgr231 dgr231 is offline Offline
Junior Poster in Training

Re: Need Help (Array)

 
0
  #9
Sep 17th, 2009
Originally Posted by firstPerson View Post
...When you pass an array to a function, and change its value you are changing the original content and not its copy.
I was unaware of this. I was under the impression that you should treat it as you would any other variable and pass either a pointer or a reference to alter it's value within a function. Thank you for clarifying.

-D
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC