Need help for little program

Nov 23, 2011 at 2:30am
Hi, i am new to C++ and I wrote a program that shows number min. to max. at screen. but it didnt work. if you help me please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<conio.h>
main()
{char c; int x[5]; int i; int j; int temp;
    for(i=0;i<5;i++)
    {
         printf("%d, input number :", i+1);
         scanf("%d", & x[i]);
         }         for(i=0;i<4;i+1)
         {
                             for(j=i+1;j<5;j++)
                             if(x[i]>x[j]){
                                           temp=x[i];x[i]=x[j];x[j]=temp;}
                                           }
         for(i=0;i<5;i++)
         printf("%d\n",x[i]);
c=getch();
}

Last edited on Nov 23, 2011 at 2:31am
Nov 23, 2011 at 2:41am
You have a couple errors:

1. you must define main() as an int; you're not giving it any type at all. It's also a good idea to include a return statement at the end of main().
2. "i+1" in your for statement is meaningless; perhaps you meant "i++"?

Correcting those should produce a functional program.
Nov 23, 2011 at 2:49am
x[5] is a serie wich has 5 elements. and i starts at 0.
i didnt want to make it 0 input number. i wanted it starts at 1 input number to five. i dont know it doesnt works for i+1 too. thx for response.
i made it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
int main()
{char c; int x[5]; int i; int j; int temp;
    for(i=0;i<5;i++)
    {
         printf("%d, input number :", i+1);
         scanf("%d", & x[i]);
         }         for(i=0;i<4;i+1)
         {
                             for(j=i+1;j<5;j++)
                             if(x[i]>x[j]){
                                           temp=x[i];x[i]=x[j];x[j]=temp;}
                                           }
         for(i=0;i<5;i++)
         printf("%d\n",x[i]);
c=getch();
return 0;
}

it doesnt works.
Nov 23, 2011 at 2:51am
and i++=i+1 ? is not it? (i m using matlab too. i m sory if i m wrong.)
Nov 23, 2011 at 3:00am
finally it worked.
but i have a question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
int main()
{char c; int x[5]; int i; int j; int temp;
    for(i=0;i<5;i++)
    {
         printf("%d, input number :",i+1);  /* first read down comment and then this : when i changed this i+1 to i++ it doesnt work. do you know why ?*/
         scanf("%d", & x[i]);
         }         for(i=0;i<5;i++)  /* first read here :i changed here i+1 to i++ and it worked. */
         {
                             for(j=i+1;j<5;j++)
                             if(x[i]>x[j]){
                                           temp=x[i];x[i]=x[j];x[j]=temp;}
                                           }
         for(i=0;i<5;i++)
         printf("%d\n",x[i]);
c=getch();
return 0;
}

Nov 23, 2011 at 3:19am
OK, let's have a little talk about operators and expressions.

i + 1 is an expression. It evaluates down to a value, but...that value doesn't "go anywhere" unless you *assign* that expression to something like this:

j = i + 1;

In this case, j will take the value of whatever i is at the moment, plus one.

In your printf() statement, since you want the numbers to be 1-based instead of 0-based, i + 1 is correct. Note that this doesn't change your value of i, which is important, because i is your loop counter.

OK so far?

Now, i++; represents an operator. It WILL change the value of i (incrementing it by 1). This is definitely not what you want in your printf() statement, but it's most assuredly what you DO want in your for loop. If you don't change the value of i, your loop will never exit, in fact, it will just do the same thing over and over again, since i never changes.

So, you now are using both correctly.

Does this help?
Nov 23, 2011 at 3:22am
Really thank you. i got it. have a nice day.
Topic archived. No new replies allowed.