Question:
I need help with a C++ Program!?
Cathy
2012-11-21 05:29:14 UTC
So i'm studying introductory to programming and have been given this for my last assignment. As i'm in first year it shouldn't be anything complicated but it is for me!

Honestly, I don't even know where to begin. I've searched the whole internet for clues and my lecturer's notes are a joke and i'm seriously so desperate right now! I've made an attempt at it, but i'm not even sure if it should look like this because i have to include a sorting array and i don't know how to do that.

Write a program that:
•1) Creates an array containing 100 random double numbers in the [0,250) range;
•2) Sorts them in descending order;
•3) Prints the contents of the sorted vector out. The sorting should be coded as a separate function double* sort_array(double* ).

The program i made prints 100 random numbers from 1-250, i need it to do that in a descending order.
please help!

#include
#include

using namespace std;
//no idea where to put a sorting array.
int main()
{
double nums[100];
int a, b, t;
double size=100;

for (t=0; t for (t=0; t cout<< "\n";

for (a=1; a for (b=size-1; b>=a; b--) {
if (nums[b-1] > nums[b]) {
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
}

for (t=0; t cout<< "\n";

return 0;
}
Three answers:
cja
2012-11-23 05:17:08 UTC
If you don't know how to sort, you should choose a fairly simple sorting algorithm, learn about it, and implement it. Here's one:



http://en.wikipedia.org/wiki/Selection_sort



Your instructor gave you a bad prototype for the sort function. When a function's argument is a pointer to an array, you also need to provide the size. A function is not very useful if it contains a hardcoded array size. Also, I don't know why sort_array is returning a double*. It should be sorting the array in place, and does not need to return anything, so the prototype is:



void sort_array(double *, size_t);



It seems like you have a decent grasp on the basics of writing C++ code. Study that description of selection sort (or any other of the many sorting algorithms), so you can write code with an understanding of what you're trying to do, and you should be fine.
Anas Imtiaz
2012-11-21 06:05:59 UTC
You need to write a separate function to do this (says so in your question).



Here are a few pointers:

1- Pass the array to the function in main()

2- You should know how to swap two variables. The following code shows how you can swap two integers:



int temp = first_number;

first_number = second_number;

second_number = temp;



the above code will swap the values of the two integers first_number and second_number such that first_number contains the value that was in second_number. Same goes for the other variable.



If you know how to compare two numbers to find out which one is the smaller, you can easily use the above code to sort your array in descending order.



Using the above code, try to this in your own. If you still have issues, feel free to email or IM me.



P.S. C/C++ allows you to declare variables with long names (without spaces). You should use the facility and name your variables such that they are easier to understand for others (who are helping you such as myself).
icefyre
2012-11-21 09:07:30 UTC
The spec isn't that clear because the instructor uses the word vector for the third part which is a different type of data structure (it's a dynamic array-like object from the STL libraries). When the instructor said you need to create the array, I didn't know if that meant to create the array on the heap or to just initialize an existing array on the stack. I assumed that the instructor meant to use the array and not a vector and that the array should be created on the heap (assumed that because of the pointer notation used in the prototype for part 3) so here you go:



#include



using namespace std;





//function prototypes

void printarr(double*) ;//used for debugging to display the array of doubles



double* create_array(); //function creates and returns an array initialized with 100 random doubles



void swap(double*,size_t ,size_t ); //used to swap values in array



double* sort_array(double*); //used to sort array using simple, inefficient bubble sort





//main program

int main()

{

//initialize random number seed

srand((unsigned)time(0));



double* arr = create_array(); //create an array of doubles and store in arr variable



cout << endl << "=============UNSORTED==============" << endl;



printarr(arr); //print unsorted array



cout << endl << "=============SORTED==============" << endl;

arr = sort_array(arr); //sort array



printarr(arr); //print sorted array



delete[] arr; //deallocate array from memory, not needed but good practice



return 0;

}



//used for debugging to display the array of doubles

void printarr(double* myarray)

{



for (size_t i = 0; i < 100; i++)

cout << myarray[i] <
}





//function creates and returns an array initialized with 100 random doubles

double* create_array()

{

double* myarray = new double[100];

for (size_t i = 0; i < 100; i++)

// myarray[i] = (double)(rand()/250.0);

myarray[i]= ((double)rand()/(double)(RAND_MAX+1)) + rand()%249;



return myarray;

}



//used to swap values in simple ineffecient bubble sort

void swap(double* myarray,size_t index1,size_t index2)

{

double temp = myarray[index1];

myarray[index1] = myarray[index2];

myarray[index2] = temp;



}





//used to sort array using simple, inefficient bubble sort

double* sort_array(double* myarray)

{

bool swapped;

do

{

swapped=false;

for (size_t i = 1 ; i < 100; i++)

{

if (myarray[i-1] > myarray[i])

{

swap(myarray,i-1,i);

swapped = true;

}

}

}

while (swapped);

return myarray;

}







Hope this helps!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...