Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   error(logic or run time error) in my program plz help! (http://www.velocityreviews.com/forums/t445641-error-logic-or-run-time-error-in-my-program-plz-help.html)

iskeletor 12-25-2006 09:44 PM

error(logic or run time error) in my program plz help!
 
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
}


Spiros Bousbouras 12-25-2006 10:33 PM

Re: error(logic or run time error) in my program plz help!
 
iskeletor wrote:
> 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.


the_init 12-26-2006 05:12 AM

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

iskeletor wrote:
> 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
}


Barry Schwarz 12-26-2006 07:02 AM

Re: error(logic or run time error) in my program plz help!
 
On 25 Dec 2006 13:44:50 -0800, "iskeletor" <zirvedoruk@gmail.com>
wrote:

>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.

> count++;


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


All times are GMT. The time now is 10:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.