943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 555
  • C++ RSS
Sep 16th, 2009
0

Need Help (Array)

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Supercharger is offline Offline
1 posts
since Sep 2009
Sep 16th, 2009
1

Re: Need Help (Array)

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
Quote 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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Sep 16th, 2009
0

Re: Need Help (Array)

//check out the code snippet, here
int random(int low, int high) {
return
}
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 16th, 2009
-1

Re: Need Help (Array)

Here is what I came up with.

"mystuff.h"
C++ Syntax (Toggle Plain Text)
  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"
C++ Syntax (Toggle Plain Text)
  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"
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Sep 17th, 2009
0

Re: Need Help (Array)

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:
C++ Syntax (Toggle Plain Text)
  1. void swap(int* firstIndex)

-D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Sep 17th, 2009
0

Re: Need Help (Array)

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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Sep 17th, 2009
0

Re: Need Help (Array)

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 :
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 17th, 2009
0

Re: Need Help (Array)

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 :
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 17th, 2009
0

Re: Need Help (Array)

...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
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Loop help - a beginners plea
Next Thread in C++ Forum Timeline: can not make out the code fragment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC