hey, i'm supposed to prompt a user to enter 6 sets of numbers, find the difference of each set based on which number is greater(so that no negative number will be seen, enter the difference into the array, rearrange the array in ascending order and then print the array.
the problem im getting is that it is accepting the numbers but not finding the difference or assigning it to points in the array...can anyone help me?? thanks alot.

this is the code i have so far

#include <stdio.h>
main () {

int nums[6];
int  num1 , num2, i, diff= 0, pass, min, minlock, temp; 

for (i=0; i<=6; i++){
printf (" Please enter first number :");
scanf ("%d", &num1);

printf ("please enter second number :");
scanf ("%d", num2);

if (num2 > num1);
diff = (num2- num1)
else diff = (num1 - num2);
scanf ("%d", &nums[i] );
}
for (pass=0; pass<=6 -1; pass++){
    min= 3200;
    for (i = pass;i<=6-1;i++){
        if (nums[i]< min){
          min= nums[i];
          minlock= i;
}
temp = nums[pass];
nums[pass];
nums[minlock]= temp;
}
for (i=0; i<6;i++)
printf ("%sd", nums[i]);
return (0);
        
}

}

Recommended Answers

All 3 Replies

line number 17

scanf ("%d", &nums[i] );

no need of this scanf, you have too use this

nums[i]=diff;

if i understand ur problem then it will help u otherwise please explain your problem .........

commented: thanks for the help, im ashamed i didnt see such a amall error +0

This looks like it's C, not really C++. Anyway, some points:

  • I think that line 12 should be scanf ("%d", &num2); , instead of scanf ("%d", num2); .
  • You have an extra ';' on the end of line 14
  • Line 17 gets a number from the user and puts it in the difference array, is that what you want? It seems like you should follow prvnkmr194's direction here.
  • Line 27 currently does nothing.
  • I think line 31 should probably be printf ("%d", nums[i]); , not printf ("%sd", nums[i]);

Some general points are:

  • Use more descriptive variable names! A good reason for this is that you (and others that have to use your code) will read the variable name much more often than you have to type it, so you can afford to have pretty long names. I'd go for things like: iFirstUserInputNumber , iSecondUserInputNumber , differences , etc.
  • Control the scope of your variables. If a variable is just used in a loop, such as your variables i and pass , then declare them like this:
    for( int i = 0; i < MAX; ++i)

    If you're only using a variable inside a loop/conditional block (such as if ) then declare the variable inside that block, eg. num1 , num2 & diff could be declared on line 8.

  • Don't use hard-coded numbers for loops/array sizes. For example, you should do:
    const int iMaxUserNumbers = 7;
    int differences[ iMaxUserNumbers ]
    /* ... */
    for( int i = 0; i < iMaxUserNumbers; ++i )
    /* ... */

    That way, if you suddenly decide that you want to let the users enter 15 numbers, you won't have to try and find a bunch of numbers that need changing.

Hope that helps. Good work, keep at it! :o)

commented: thank you, everything runs smoothly now +0

thank you to everyone, the program runs smoothly now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.