| | |
Need Help (Array)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 1
Reputation:
Solved Threads: 0
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
Excerise::
http://www.lhs.logan.k12.ut.us/~rweeks/cp1/array.htm
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
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.
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.
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.
//check out the code snippet, here
int random(int low, int high) {
return
}
int random(int low, int high) {
return
}
C++ Syntax (Toggle Plain Text)
//use logic to find the min element //assign initial min element to A[0]; //search through loop seeing if other element's value are //greater than min element value if so the reassign min element //other wise keep searching int getMin(int A[], int max) { ... } //same as min, but use ">" when comparing int getMax(int A[], int max) { ... } //create a temp variable and assign it to first //assign second to first //assign temp to first void swap(int &first, int &second) { ... }
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
Here is what I came up with.
"mystuff.h"
"mystuff.cpp"
"array.cpp"
"mystuff.h"
C++ Syntax (Toggle Plain Text)
#ifndef MYSTUFF_H #define MYSTUFF_H void randomize(); int random( int high, int low = 0); int getMax( int A[] ); int getMin( int A[] ); int swap( int A[] ); #endif
"mystuff.cpp"
C++ Syntax (Toggle Plain Text)
#include <time.h> #include <stdlib.h> #include "mystuff.h" void randomize() { srand(time(NULL)); } int random( int high, int low ) { return rand() % (high-low) + low; } int getMax( int A[] ) { int max = A[0]; for( int i = 1; i < 10; i++ ) { if( A[i] > max ) { max = A[i]; } } return max; } int getMin( int A[] ) { int min = A[0]; for( int i = 1; i < 10; i++ ) { if( A[i] < min ) { min = A[i]; } } return min; } int swap( int A[] ) { for( int i = 0; i < 10; i++ ) { int temp = A[i]; int ran = random(10); A[i] = A[ran]; A[ran] = temp; } return A[10]; }
"array.cpp"
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "mystuff.h" using namespace std; int main() { //declare variables and set random seed int A[10], min, max; randomize(); //make random numbers and assign to array for(int i = 0; i < 10; i++) { A[i] = random(100, -100); } //find min and max numbers in array min = getMin( A ); max = getMax( A ); //output array before shuffled for( int i = 0; i < 10; i++ ) { if( i != 9 ) { cout << A[i] << " "; } else { cout << A[i] << endl; } } //output min and max values cout << "Low: " << min << "\nHigh: " << max << endl; //shuffle the array swap( A ); //output shuffled array cout << "Shuffled: "; for( int i = 0; i < 10; i++ ) { if( i != 9 ) { cout << A[i] << " "; } else { cout << A[i] << endl; } } system("PAUSE"); return 0; }
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:
-D
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)
void swap(int* firstIndex)
-D
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.
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)
void *swap( int A[] ) { for( int i = 0; i < 10; i++ ) { int temp = A[i]; int ran = random(10); A[i] = A[ran]; A[ran] = temp; } }
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 :
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.
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)
int swap( int A[] ) { for( int i = 0; i < 10; i++ ) { int temp = A[i]; int ran = random(10); A[i] = A[ran]; A[ran] = temp; } return A[10]; }
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
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 :
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.
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)
int swap( int A[] ) { for( int i = 0; i < 10; i++ ) { int temp = A[i]; int ran = random(10); A[i] = A[ran]; A[ran] = temp; } return A[10]; }
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
•
•
•
•
...When you pass an array to a function, and change its value you are changing the original content and not its copy.
-D
![]() |
Similar Threads
- How can is see what button is pressed in a button array (VB.NET)
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Loop help - a beginners plea
- Next Thread: can not make out the code fragment
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







