0

So I have to create a program that will first ask the user how many numbers will he enter(up to 20). After that user inputs his numbers: For example (input):

9

1 2 3 4 5 6 7 8 9

Program has to print those numbers, assign them to static array and then input and sort them from biggest to smallest. For example(output):

before: 1 2 3 4 5 6 7 8 9

after: 9 8 7 6 5 4 3 2 1

I know how to do second part of the program. However I can't figure out how do you assign those numbers to an array while their way of input is like that: 1 2 3...

This is what I came up with when you input numbers separetly but only up to 10 numbers:

int main()
{
    int i, j, t;
    int num[10];
    scanf("%d", &num[0]);
    scanf("%d", &num[1]);
    scanf("%d", &num[2]);
    scanf("%d", &num[3]);
    scanf("%d", &num[4]);
    scanf("%d", &num[5]);
    scanf("%d", &num[6]);
    scanf("%d", &num[7]);
    scanf("%d", &num[8]);
    scanf("%d", &num[9]);
    int pos[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    for(j=0; j<10; j++)
    {
        for(i=0; i<9-j; i++)
        if(pos[i]<pos[i+1])
        {
            t = pos[i];
            pos[i] = pos[i+1];
            pos[i+1] = t;
        }

    }

    for(i=0; i<10;i++){
        printf("%d", num[pos[i]]);
        if(i!=9)
            printf("\t");
    }



    return 0;
}
Patryk Besler
  • 21
  • 1
  • 4
  • 1
    Without showing the code you already have and explaining what exactly is the problem with it, this question is off-topic. –  Oct 25 '17 at 07:26
  • Welcome to Stack Overflow! Kindly show your research / debugging effort so far. Please read [Ask] page first. – Sourav Ghosh Oct 25 '17 at 07:26
  • 1
    Welcome to Stack Overflow! I'm voting to close this question as this is clearly a write my code request without any visible effort from Patryk Besler, not a _question_. – Sourav Ghosh Oct 25 '17 at 07:26
  • you can get input values separated by space like that: scanf("%d %d %d", &var1, &var2, &var3); – Paweł Dymowski Oct 25 '17 at 07:30
  • 1
    `int n; scanf("%d", &n); /* check input */ int array[n]; for(int i = 0; i < n; ++i) scanf("%d", array + i);` – BLUEPIXY Oct 25 '17 at 07:32

2 Answers2

0

Here is the solution:

#include <stdio.h>

int main()
{
    int arraySize = 0;
    printf("How big array do you want?\n");
    scanf("%d", &arraySize);

    int array[arraySize];

    printf("Please enter %d digits separated by space:\n", arraySize);

    int i = 0;        
    while (i < arraySize && scanf("%d", &array[i++]) == 1)
         /*empty loop*/;

    for(int i = 0; i < arraySize; i++){
         printf("%d, ", array[i]);
    }
}

For getting user input separated by space I used solution from this post: Putting numbers separated by a space into an array

Paweł Dymowski
  • 840
  • 7
  • 14
0

If you know the maximum number of integers that you will input, then there is no need to dynamically allocate storage for an array unless the max number is so large that you risk StackOverflow (which is compiler dependent, but generally you can fit at least 250000 integers, see C/C++ maximum stack size of program)

Unless you are dealing with that many (you're not, you're talking 20), instead of dynamically allocating storage, simply declare a constant and declare an integer array of constant size, e.g.

#include <stdio.h>

#define MAXI 20     /* if you need a constant, define one */

int main (void) {

    int n = 0, 
        array[MAXI] = {0};
    ...

You now have array with storage for MAXI (20) integers. You have also declared a convenient constant at the top of your code, so if you need to adjust the number of integers you accept, you simply change one-number rather than picking through all declarations and loop limits to make the change.

The MOST important thing you will do is validate all user input. That means verifying that the user entered valid data, your program successfully converted the input to a value you can use in your code, and.. the value is within the proper range. You do this for EVERY input you take. For example, getting the number of integers from the user:

    printf ("how many numbers (up to %d)?: ", MAXI);
    /* validate ALL user input, including range of input */
    if (scanf ("%d", &n) != 1 || n < 1 || n > MAXI) {
        fprintf (stderr, "error: invalid input 'n'.\n");
        return 1;
    }

By checking the return of scanf you validate one integer was entered by the user and successfully converted to int and placed in the variable n. You next must verify that it is in the range of values your program will take (e.g. 1->MAXI. The entry of a negative value more than 20 would result in Undefined Behavior, so make sure the value is in the proper range. (0 just would be self-defeating for your program)

Next, get the values and store them in your array. (you are just doing the same thing n times, so use a loop! You can also dispense with the range check as long as you are entering values within the normal integer range)

    for (int i = 0; i < n; i++) {   /* read/validate 'n' integers */
        printf ("array[%2d]: ", i);
        if (scanf ("%d", &array[i]) != 1) {
            fprintf (stderr, "error: invalid input 'array[%d]'.\n", i);
            return 1;
        }
    }

(it's also nice to prompt the user reminding them which value you are expecting, e.g. "array[1]: ", etc..)

Other than outputting your array, your sort is the last thing you need to do. C provides qsort as the standard sorting routine. It is far more efficient and far better tested than anything you (or I) will write off the top of our heads -- use it in all real situations.

That said, there is nothing wrong with writing your own sort, and it is good practice. (and for 20 values or less, it is just as fast). But, use a function! If there are parts of your code that can be used more than once, write a function to help make your code more maintainable (and much easier to read). You can write a bubble-sort simply:

void bubblesort (int *arr, int size)
{
    int i, j, tmp;

    for (i = 0; i < size; i++) {
        for(j = 0; j < (size-1) - i; j++) {
            if( arr[j] < arr[j+1] ) {   /* change to > for ascending */
                tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
            }
        }
    }
}

Putting it altogether, you can do something similar to the following:

#include <stdio.h>

#define MAXI 20     /* if you need a constant, define one */

void bubblesort (int *arr, int size)
{
    int i, j, tmp;

    for (i = 0; i < size; i++) {
        for(j = 0; j < (size-1) - i; j++) {
            if( arr[j] < arr[j+1] ) {   /* change to > for ascending */
                tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
            }
        }
    }
}

int main (void) {

    int n = 0, 
        array[MAXI] = {0};

    printf ("how many numbers (up to %d)?: ", MAXI);
    /* validate ALL user input, including range of input */
    if (scanf ("%d", &n) != 1 || n < 1 || n > MAXI) {
        fprintf (stderr, "error: invalid input 'n'.\n");
        return 1;
    }

    for (int i = 0; i < n; i++) {   /* read/validate 'n' integers */
        printf ("array[%2d]: ", i);
        if (scanf ("%d", &array[i]) != 1) {
            fprintf (stderr, "error: invalid input 'array[%d]'.\n", i);
            return 1;
        }
    }

    printf ("before:");             /* output before: ... */
    for (int i = 0; i < n; i++)
        printf (" %d", array[i]);
    putchar ('\n');

    bubblesort (array, n);          /* sort your array */

    printf ("after :");             /* output after: ... */
    for (int i = 0; i < n; i++)
        printf (" %d", array[i]);
    putchar ('\n');

    return 0;
}

Example Use/Output

$ /bin/arrayinput
how many numbers (up to 20)?: 5
array[ 0]: 1
array[ 1]: 2
array[ 2]: 3
array[ 3]: 4
array[ 4]: 5
before: 1 2 3 4 5
after : 5 4 3 2 1

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85