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

Recommended Answers

All 8 Replies

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

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. :D
:)

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.

commented: Well said. +27

//check out the code snippet, here
int random(int low, int high) {
return
}

//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) {
  ...
}

Here is what I came up with.

"mystuff.h"

#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"

#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"

#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;	
}
commented: Since when do we provide free cookies? -4

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:

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.

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 :

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];
}

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.

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 :

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];
}

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.

...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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.