473,508 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
}

Dec 25 '06 #1
3 1272
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.

Dec 25 '06 #2

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
}

Dec 26 '06 #3
On 25 Dec 2006 13:44:50 -0800, "iskeletor" <zi********@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
Dec 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3996
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
1
3538
by: Bimal | last post by:
Hi, I upgraded my gcc from 2.95 to 3.3. When I compile some projects I get error messages saying... /usr/local/include/c++/3.3/ctime:68: error: `tm' not declared...
5
1446
by: rajavel | last post by:
Hi, Runtime Error occurs after occurs after you execute a program. Example is StackOverFlow, Invalid Pointer reference Compile Time Error - If this Error occurs you will not be able to...
12
2832
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
669
25395
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
35
3738
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
1149
by: hafeez | last post by:
Hello All, I created two database instances in the same MS SQL SERVER 2005 named hafeez and local. I created a linked server from hafeez database to local named HTOL I created a dummy table in...
1
2793
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
0
966
by: make me rain | last post by:
hai, i am very new ti VB6. how can i show i.e log All the err.number , err.description, error time on a simple VB6 application (it is NOT a data base app) like creating text boxes for each error...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.