Hi, I was wonding if anyone could please help me with my programming. I got most of my programming down. I just need help with my output. Currently I have:

Enter a number:

9 < ==the amount I want

Enter 9 numbers:

1 1 2 9 3 2 5 1 9 <== numbers that I inputed

1 happened 0 times.

1 happened 0 times.

1 happened 0 times.

2 happened 0 times.

2 happened 0 times.

3 happened 0 times.

5 happened 0 times.

9 happened 0 times.

9 happened 0 times. < == my current output

But what I really want is:

1 happened 3 times.

2 happened 2 times.

3 happened 1 times.

5 happened 1 times.

9 happened 2 times. < == expected output.

Can anyone please help me.

my code so far is

double number[1000];

int n;

int i;

int smallest;

int j;

int tempvalue;

for(i = 0; i < 1000; i = i + 1){

number[i] = 0;

}

cout << "Enter a number.\n";

cin >> n;

cout << "Enter " << n << " numbers:\n";

i = 0;

while(i < n){

cin >> number[i];

i = i + 1;

}

i = 0;

while(i < n-1){

smallest = i;

for(j=i+1;j < n; j = j+1){

if (number[smallest] > number[j]){

smallest = j;

}

}

tempvalue = number[i];

number[i] = number[smallest];

number[smallest] = tempvalue;

i = i + 1;

}

for(i = 0; i < n; i = i + 1){

cout<< number[i] << " happened " << number[smallest] << " times.\n";

}

Enter a number:

9 < ==the amount I want

Enter 9 numbers:

1 1 2 9 3 2 5 1 9 <== numbers that I inputed

1 happened 0 times.

1 happened 0 times.

1 happened 0 times.

2 happened 0 times.

2 happened 0 times.

3 happened 0 times.

5 happened 0 times.

9 happened 0 times.

9 happened 0 times. < == my current output

But what I really want is:

1 happened 3 times.

2 happened 2 times.

3 happened 1 times.

5 happened 1 times.

9 happened 2 times. < == expected output.

Can anyone please help me.

my code so far is

double number[1000];

int n;

int i;

int smallest;

int j;

int tempvalue;

for(i = 0; i < 1000; i = i + 1){

number[i] = 0;

}

cout << "Enter a number.\n";

cin >> n;

cout << "Enter " << n << " numbers:\n";

i = 0;

while(i < n){

cin >> number[i];

i = i + 1;

}

i = 0;

while(i < n-1){

smallest = i;

for(j=i+1;j < n; j = j+1){

if (number[smallest] > number[j]){

smallest = j;

}

}

tempvalue = number[i];

number[i] = number[smallest];

number[smallest] = tempvalue;

i = i + 1;

}

for(i = 0; i < n; i = i + 1){

cout<< number[i] << " happened " << number[smallest] << " times.\n";

}

This code is already invalid

i= 0;

while(i < n-1){

Let assume that n = 1 then you will have

while ( 0 < 0 )

so this loop will be executed never.

i= 0;

while(i < n-1){

Let assume that n = 1 then you will have

while ( 0 < 0 )

so this loop will be executed never.

Topic archived. No new replies allowed.