sorting

Pages: 12
i was wondering my prof was asking me to do this two programs and im not sure how to can someone help me?

the first is:

write a program that will input "X" times of numbers and will display the highest average and lowest.

the 2nd one is:

write a program that will input "10" names and will display them alphabetical order.
1.)
You'll need a for loop (or a while loop) and three variables for the highest, average, and lowest. When the user inputs the first number, assign all three to that number, then go into your loop. In your loop add the next number to average, check to see if it's lower than lowest or higher then highest, and apply the correct operation. Repeat. After you're done, divide the average (which is a sum at this point) by the number of numbers entered. Display.

2.)
There are so many ways you could implement this, you should tell me your ideas first.
o.k here is the deal

in

1.) give me a sample code/syntax? uhm.. i kinda had gone as far as doin all except for the part of displaying the high and low.

i kinda have no idea on how will i assign the inputs of the user.

2.)

my i dea would be like this..

the user will input 10 names (the code will be a loop one)
n1 will determine how many names the user will input
like for(i=0;i<=n1;i++)
and it will be displayed ALPHABETICALLY ARRANGED

like for example

if he enter 3 names and it goes like this

how many names: 3
enter name: robert
enter name : matt
enter name : cedrick

and the final cout will be like this

name list:

cedrick
matt
robert

(arranged alphabetically)
2.)

Ever hear of Vectors?

1.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int numOfNums;
int high;
int low;
int sum;

//get number of numbers

//get first number
//set all above variables to that input

for(int x = 1; x < numOfNums; x++)
{
    //get input
    //if number is highest or lowest, set the correct one
    //add input to sum
}

//display output 
1.)
im kinda not sure on how this will go and im kind of lost with your syntax >.<

and im really unsure on how to do the line 14 and 15 so as im lost with line 6-9

and here's what ive done its an incomplete one but look on how does the product goes im kinda confused on where and how should i place the product >.<

1
2
3
4
5
6
7
8
9
10
int i,a1,num1,high,low,sum,prod;
    cout<<"how many numbers?"<<endl;
    cin>>a1;
    for(i=1;i<=a1;i++)
    {
    cout<<"Enter the number: ";
    cin>>num1;
    }
    prod = num1*num1;
    cout<<prod<<endl;



2.)

nope XD and btw our lesson was only array,do..while, if and else statements.

so i am assuming that the program will run using does control structures and loops.
Last edited on
1.)

You're going to have to try to work with me. That was some generous skeleton code.

6 Tell user to enter the number of numbers they are going to enter, then get input from the user (2-3 of your code).

8 Tell user to enter the first number. Get input from the user.

9 Set the highest, lowest, and sum variables to that number.

14 If the number the user input is greater than the highest number, set the highest to that number. If the number the user input is less than the lowest number, set the lowest to that number.

15 Add the number to the running sum.

When you are done with the for loop, the average is the sum divided by the number of numbers.

2.)

Declare an array of 10 strings. Get the user input for each String. Sort the array using your own defined sort method, or look up a sort method. I can't remember which one comes with all versions of C++.
reply to your number 1.)

thats the problem i dont know how to do that :( our prof was kinda "hey do...while is for loop control structure, so make a program using it" and we were just like "how the hell would we do that if you just say that do...while is a loop control structure."

that's what our situation in school is kind of. so i really have no idea XD

2.

array of 10 strings? like:
1
2

int = n[10];

would it be like that? err.. im not so sure about it XD

about the sort method thats THE REAL DEAL our prof ddnt even bother to told us how to use or make a sorting program all he says that a control structure can also be use for sorting in methods of binary and etc. -_-
1.)

do while loops are iteration structures. They are like the while loop, but they are post-test (meaning they test the condition after the iteration.

I really don't know what you don't understand about the last response. If you could be more specific, that would be nice.

2.)

 
int n [10];


You can google sort methods or learn how to create your own.

I like insertion sort as an easy way to implement my own sorting function. You could try that.
i am really kind of lost XD

i really hope you dont mind giving me a sample syntax for number 1?

and as for no. 2 i dont know how would array help me with the sorting in alphabetical order.
I really can't give you any more on number one without giving you the answer. Your code has all the syntactical stuff, so you just need to use what you know to get to what I told you about.

An array allows you to store the names without using ten variables and there are sort functions that you can use to sort an array. You can't sort variables.
oh ok XD

but in number 1.) i kind of having a problem with the product

you see my intention in line 9 was to multiply all the numbers that the user inputs but as i try to run the program i dont know where the heck the program gets his multiplier, the program gave me a product of i dont know where did it come from.

number 2.) uhm i dont know how would array help me here..

you see i really dont know how would the user inputs the name, lets say he inputs
x
c
a
d
h
b
s

with that given input how would the array work to sort that and make it alphabetical arrange that would look like this.

a
b
c
d
h
s
x
1.)
What average are you trying to get? Your code just gives you the square of your last number.

To add numbers to sum
[code]
sum += num;
[code]

You need that in your loop

2.)
Make an array of strings and try to sort them.
I do want to point out that you will most likely want to use a while loop since that is what your professor is asking for. How you go about making that while loops all depends on your taste.

I can't add on to GRex's code without, again, giving you the answer -- But what I can say is I think you are losing sight and relying too much on your professor to tell you what to do. Remember that in programming you are going to have to think outside the box. From the instructions you have presented to us, I see no need for the product of any of the numbers.

As for your second problem.. You will find it more beneficial to create your own alphabetizing system versus looking one up. An array is going to help you because it is a lot cleaner and more organized than making 10 different variables (Which would be a lot of coding that you don't really need).

I am curious to know, do you know what an array is?
on number one i actually had made the program to be honest but the only problem i got is the product and displaying the highest and lowest

and as for array...

im thought of my prof that an array can be used so that you dont have to declare a lot of assignments and that it can be used for sorting (to which he ddint tell us how), that's all nothing more.
The program your prof wants is the average, so where is the product coming from?

An array by itself will not do the sorting for you, you have to figure that part out. Did your professor tell you anything about how to use arrays in any way shape or form? I would assume so, and if not there are tutorials that can help you:

http://www.cplusplus.com/doc/tutorial/arrays/
oh i forgot to tell in the first post that he was also looking for product sorry for that

actually he ddint on the part of array..
He wants the lowest, average, highest, and product of all the numbers? Just create another variable that does what sum does, except it gets the product.

 
product *= num;

in the loop.

Use the tutorial, make an array of ten strings, sort using any sorting algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main()
{
    int i,a1,num1,high,low,sum,prod,ave;
    cout<<"a"<<endl;
    cin>>a1;
    for(i=1;i<=a1;i++)
    {
    cout<<"Enter the number: ";
    cin>>num1;
    sum=num1+num1;
    prod=num1*num1;
    ave=sum/a1;
    }
cout<<prod<<" "<<sum<<" "<<ave<<endl;
system("pause");
return 0;
}


here is the sample syntax i made for number 1 without the high and low..

i have problem there..

the prod sum and ave dont display the right numbers...

can someone help me with that?

and as for number 2.

i think i know how would i do it.. ^_^
Last edited on
I knew you could do it. Just a few problems with your implementation. I edited your code below. You will have to do the product yourself. What I did with the variables isn't important (except for the sum and product). I just like declaring my variables on their own line and keeping temporary variables temporary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
using namespace std;
int main()
{
    //int i,a1,num1,high,low,sum,prod,ave;
    int a1;
    int num1;
    int high;
    int low;
    int sum = 0;
    int prod = 1;
    int ave;

    cout<<"a"<<endl;
    cin>>a1;
    for(int i=1;i<=a1;i++)
    {
    cout<<"Enter the number: ";
    cin>>num1;
    //sum=num1+num1;  when the loop finishes you'll just get the last number * 2
    sum += num1;
    //prod=num1*num1;  when the loop finishes you'll just get the last number squared.  Can you use the solution for sum to fix this?
    //ave=sum/a1;  wrong place for this
    }

    //this should be here
    ave = sum / a1;

    cout<<prod<<" "<<sum<<" "<<ave<<endl;
    system("pause");

     return 0;
}


To implement your high and low, you'll need to put one of the inputs outside the loop, then set high and low to the first input. You'll adjust them as needed in the loop.
@grex2959

o. k so i have to set an initial value for them to work.

i get them to work properly.

just the high and low this time.

how would i put one of the inputs outside the loop?
then set high and low to the first input?
and adjust them as needed in the loop?

would it be like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout<<"a"<<endl;
    cin>>a1;
    for(int i=1;i<=a1;i++)
    {
    cout<<"Enter the number: ";
    cin>>num1;
    //sum=num1+num1;  when the loop finishes you'll just get the last number * 2
    sum += num1;
    //prod=num1*num1;  when the loop finishes you'll just get the last number squared.  Can you use the solution for sum to fix this?
    //ave=sum/a1;  wrong place for this
    }
cout<<"enter...."; //should it go like this?
cin>>n2;               // this is the outside of the loop input
ave = sum/a1;


if it would be like that

how would the if and else go?


1
2
3
4
5
6
7
8
9
10
if(n1>n2)
n1=high;
else
n1=n1;
if(n1<n2)
n1=low;
else
n1=n1;

cout<<high<<" "<<low<<endl;


would it be like that?


and ooh btw i still have no.2 as a problem i havent started that yet i would be really thankful if you could help me on that too but of course after i have solved no.1 XD

btw i have read about the sorting of the array

there is this flag and temp

1
2
3
temp=n1[];
n1[]=n2[];
n2[]=temp;


im not sure if that it is how suppose to go cuz i havent placed may attention to number 2. yet

btw im not sure what's this temp and flag for XD
Last edited on
Pages: 12