error(logic or run time error) in my program plz help!

I

iskeletor

i want to have integers from user and take them into a array then
sorting them but where do i make mistake?thanx from now

#include <stdio.h>
#include <stdlib.h>

int number=0,count=0,numberArray[100];
int main (){
int a;
Sort(numberArray);
for(a=0;a<count;a++){
printf("%ld\t",numberArray[a]);
}
return 0;
}
int Sort(int array[]){
int temp,z,t;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
while(number!=-1){
numberArray[count]=number;
//printf("numberArray[%ld]=%ld",count,numberArray[count]);
count++;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
printf("%d\n",count);
for(t=0;t<count;++t){
for(z=t+1;t<count+1;++z){
if(array[t]>array[z]){
temp=array[t];
array[t]=array[z];
array[z]=temp;
}
}
}
}//while loop ends
}
 
S

Spiros Bousbouras

iskeletor said:
i want to have integers from user and take them into a array then
sorting them but where do i make mistake?thanx from now

It is useful to explain what goes wrong when you run
your programme.
#include <stdio.h>
#include <stdlib.h>

int number=0,count=0,numberArray[100];
int main (){
int a;
Sort(numberArray);

You are calling a function which you have not declared or
defined up to this point in the programme.
for(a=0;a<count;a++){
printf("%ld\t",numberArray[a]);

numberArray[a] is an int not a long int therefore the
format to use is %d not %ld. You repeat a similar mistake
at several places either with printf or with scanf.
}
return 0;
}
int Sort(int array[]){
int temp,z,t;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
while(number!=-1){
numberArray[count]=number;
//printf("numberArray[%ld]=%ld",count,numberArray[count]);
count++;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
printf("%d\n",count);

You don't put the number which has just been read
into the array before you do the sorting.
for(t=0;t<count;++t){
for(z=t+1;t<count+1;++z){

Is the condition meant to be z<count+1 ? Even so
it's wrong because you have not put any useful value
at numberArray[count].
if(array[t]>array[z]){
temp=array[t];
array[t]=array[z];
array[z]=temp;
}
}
}
}//while loop ends
}

You have defined Sort as returning int but it doesn't
actually return anything.
 
T

the_init

iskeletor said:
i want to have integers from user and take them into a array then
sorting them but where do i make mistake?thanx from now

#include <stdio.h>
#include <stdlib.h>

int number=0,count=0,numberArray[100];
int main (){
int a;
Sort(numberArray);
for(a=0;a<count;a++){
printf("%ld\t",numberArray[a]);
}
return 0;
}
int Sort(int array[]){
int temp,z,t;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
while(number!=-1){
numberArray[count]=number;
//printf("numberArray[%ld]=%ld",count,numberArray[count]);
count++;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
printf("%d\n",count);
for(t=0;t<count;++t){
for(z=t+1;t<count+1;++z){
if(array[t]>array[z]){
temp=array[t];
array[t]=array[z];
array[z]=temp;
}
}
}
}//while loop ends
}



Try this


#include <stdio.h>
#include <stdlib.h>

int number=1,count=0,numberArray[100];

void Sort(int array[]);



int main ()
{
int a;
Sort(numberArray);

for(a=0;a<count;a++)
{
printf("%ld\t",numberArray[a]);
}
return 0;
}



void Sort(int array[])
{
int temp,z,t;

while(1)
{
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);
if(number!=-1)
{
numberArray[count]=number;
count++;
}
else
{
for(t=0;t<count;++t)
{
for(z=t+1; z<count-1; ++z)
{
if(array[t]>array[z])
{
temp=array[t];
array[t]=array[z];
array[z]=temp;
}
}
}
return;
}


}//while loop ends
}
 
B

Barry Schwarz

i want to have integers from user and take them into a array then
sorting them but where do i make mistake?thanx from now

#include <stdio.h>
#include <stdlib.h>

int number=0,count=0,numberArray[100];
int main (){
int a;
Sort(numberArray);

At this point, Sort is an unknown function. You should either code a
prototype for it (right after the #include directives) or place the
code for main after the code for Sort.
for(a=0;a<count;a++){
printf("%ld\t",numberArray[a]);

%ld is not suitable for an int in printf.
}
return 0;
}
int Sort(int array[]){

You have confused yourself by having the Sort function also perform
the input. It would be better to separate the two tasks in different
functions.
int temp,z,t;
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);

%ld is not suitable for an int in scanf either.
while(number!=-1){
numberArray[count]=number;
//printf("numberArray[%ld]=%ld",count,numberArray[count]);

// comments can wrap when posted, preventing others from compiling
your code.

After the first number, count will be 1.
printf("Enter a positive integer(To exit enter -1)\n");
scanf("%ld",&number);

You probably meant to terminate the while loop at this point. You
certainly don't want to sort the array after each number is input.
This is an example of how consistent indenting can really help you.
printf("%d\n",count);
for(t=0;t<count;++t){

t is 0, count is 1.
for(z=t+1;t<count+1;++z){

t does not change in this loop so the loop will never end. Did you
mean z<count+1? As written, z will be 1.

You might also consider a little horizontal white space to make things
easier to read.
if(array[t]>array[z]){

array[1] contains 0 (it was initialized because the array is at file
scope. It does not contain a number input to the program by the user.
You are sorting numbers you don't have yet.
temp=array[t];
array[t]=array[z];
array[z]=temp;
}
}
}
}//while loop ends

Too late.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top