## KellieD

Hi,

Would be very grateful if someone could tell me how to start an array by getting the program to ask you how many values you want to enter (the maximum being 50)

Thanks :)

## KellieD

Okay, so this is what I need to do;

Create a program in which a user is asked for:
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array of double.
Sorts the values in descending order.
Prints the sorted array to the console.

And this is what I have so far;

``````#include <iostream>

using namespace std;

int main()
{
int (num);

do {
cout << "Enter a value:";
cin >> num;
} while (num !=50);

return 0;
}``````

And the code works, but I just need to figure out the bit where it asks to to enter a maximum of 50 numbers before I can put in like a BubbleSort thingy or something!

(Oh and I know that the while (num!=50) part is wrong and this is why i need help!)

I need to have it done by midnight tomorrow so any help as soon as possible would be greatly appreciated!! :)

## vanalex

Hi,

Would be very grateful if someone could tell me how to start an array by getting the program to ask you how many values you want to enter (the maximum being 50)

Thanks :)

Hello!
One way to do this is with a dynamic array. I'll give you an example so as to adapt it in your program..

``````#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

srand(time(0));
int size;
cout << "hello user, enter the size of your array: ";
cin >> size;
int *arr = new int[size];
cout << "array was filled with random numbers.." << endl;

for (int i = 0; i < size; i++) {
arr[i] = rand() % 100;  //the array is filled with random numbers from 0 - 99
cout << arr[i] << " ";
}
cout << endl;
delete [] arr;
return 0;
}``````

## KellieD

Thanks a million :)

## vanalex

Okay, so this is what I need to do;

Create a program in which a user is asked for:
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array of double.
Sorts the values in descending order.
Prints the sorted array to the console.

And this is what I have so far;

``````#include <iostream>

using namespace std;

int main()
{
int (num);

do {
cout << "Enter a value:";
cin >> num;
} while (num !=50);

return 0;
}``````

And the code works, but I just need to figure out the bit where it asks to to enter a maximum of 50 numbers before I can put in like a BubbleSort thingy or something!

(Oh and I know that the while (num!=50) part is wrong and this is why i need help!)

I need to have it done by midnight tomorrow so any help as soon as possible would be greatly appreciated!! :)

Will the user define the size of the array or not? In the example i wrote above the size is being defined by the user..so if you want to reject any size that exceeds 50 then your loop should look like:

``````do {

cout << "enter the size of your array: ";
cin >> size;

}  while (size > 50 || size < 0);``````

## KellieD

``````#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

srand(time(0));
int size;
cout << "hello user, enter the size of your array: ";
cin >> size;
int *arr = new int[size];

for (int i = 0; i < size; i++);
int (num);

do {
cout << "Enter a value:";
cin >> num;
} while (size!=50);

return 0;
}``````

Okay, just wondering if ya could tell me where I'm going wrong because I tried doing that ^ but it just asks me to enter the size and enter a number but it isn't on a loop :S

## KellieD

ohright sorry, didn't see your last comment,
ehm I'm not sure :S
I'll try it that way and see what way it turns out..
Thanks again :)

## KellieD

Okay I tried this;

``````int main() {
int (size);
int (num);

do {
cout << "enter the size of your array: ";
cin >> size;
cout << "Enter a value:";
cin >> num; }
while (size > 50 || size < 0);

return 0;
}``````

and the same thing happened as with the other one!
Any ideas?

## vanalex

Okay I tried this;

``````int main() {
int (size);
int (num);

do {
cout << "enter the size of your array: ";
cin >> size;
cout << "Enter a value:";
cin >> num; }
while (size > 50 || size < 0);

return 0;
}``````

and the same thing happened as with the other one!
Any ideas?

Don't ask the user to give you the size of the array and to fill the array in the same loop. This is your mistake..Do it like this:

``````#include <iostream>

using namespace std;

int main() {

int size;
do {
cout << "hello user, enter the size of your array: ";
cin >> size;
} while (size > 50 || size < 0);

double *arr = new double[size];
cout << "now fill the array with " << size << " values: " << endl;

for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "the content of the array is: " << endl;

for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
delete [] arr;
return 0;
}``````

## KellieD

Thank you thank you thank you thank you thank you thank you sooooooooooo much :)

## KellieD

Now just one more thing, for this bubblesort thingy,do I just put in something like this?

``````for (a=1; a<size; a++) {
for (b=size-1; b>=a; b--) {
if (nums[b-1] > nums[b]) { // if out of order
//exchange elements
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
}``````

## vanalex

Now just one more thing, for this bubblesort thingy,do I just put in something like this?

``````for (a=1; a<size; a++) {
for (b=size-1; b>=a; b--) {
if (nums[b-1] > nums[b]) { // if out of order
//exchange elements
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
}
``````

end quote.

Adapt this piece of code you wrote, in the code i wrote and check if it works.

## KellieD

vanalex, thanks a million :)