POST-A-PROBLEM

Anyone can post their programming problems and anyone also can contribute an answer in this page.

7 comments:

Anonymous said...

#include
#include

int main(){

int a[5]={5,4,3,2,1};
int temp, x, y, z;

for (x=0 ; x<4 ; x++){
for (y=1 ; y<4 ; y++){
if (a[x] > a[y]){
temp = a[y];
a[y] = a[y+1];
a[y+1]=temp; }

}

for (x=0 ; x<4 ; x++){
printf("%d ", a[x]);
}

getche();

}

getche();
}

ano error nito ?
dapt output nito ay 12345.

Juan Tamad said...

basically, your implementation of bubble sort algorithm is wrong and you only used 4 out of 5 elements in your array because of <4 so it simply means you will just get the value of array element 0 to 3 only which is wrong..try this code..
#include
#include

int main(){

int a[5]={5,4,3,2,1};
int temp, x, y;
for (x=0 ; x<5 ; x++){
for (y=0 ; y<4 ; y++){
if (a[x] < a[y])
{
temp = a[x];
a[x] = a[y];
a[y]=temp;
}
}
}

for (x=0 ; x<5 ; x++)
printf("%d ", a[x]);

getche();
}

Anonymous said...

got it thanks! ;D

Anonymous said...

#include
#include

main(){

int array[10];
int i;
int even=0;
int odd=0;

printf("enter array number: \n");
scanf("%d ", &array[i]);

for (i=0 ; i<10 ; i++){
if(array[i]%2==0)
even++;
else
odd++;
}

if (even>odd)
printf("even is the majority");
else if (even>odd)
printf("odd is the majority");
else
printf("they are equal");

getche();
}

the output of this is if you input
2 4 6 8 10 12 1 3 7 5
the output would be even is majority
and if you input more odd nums than even the output wouldbe odd is the majority
and if there are equal numbers of odd and even output would be they are equal.what's wrong with my program?

Juan Tamad said...

Problems of your program:
1. There should be a loop before scanf to be able to assign 10 numbers inside your array.

printf("enter array number: \n");
for (i=0 ; i<10 ; i++)
scanf("%d ", &array[i]);

2. Your conditions are the same.
if (even>odd)
printf("even is the majority");
else if (even>odd)
printf("odd is the majority");
else
printf("they are equal");

it should be:
if (even>odd)
printf("even is the majority");
else if (even<odd)
printf("odd is the majority");
else
printf("they are equal");

Red Thinker!!!;D said...

Kanang Senpai Unsay Gamit Sa Extern Sa C/C++ Programming Language?!!!;D

Anonymous said...

source tbangi ko ninyo sa SORTING ALGORITHMS!!! :( DI JUD KO KASABOT SA INSERTION AND SELECTION . PLEASE HELP MO! MGA BRIGHT NA BTAW MO.. :(