0

I just learned how arrays works and I'm trying to find the maximum element in an array, but I can't get all the values I want into the array. I tried this way:

int x[10];

for (i = 1; i <= 10; i++)
{
    printf("please enter a number: ");
    scanf_s("%d", x[i]);
}

Below is the whole program

#include <stdio.h>

int main(void)
{
    int i = 1;
    int max, min;
    int x[10];

    for (i = 1; i <= 10; i++)
    {
        printf("please enter a number: ");
        scanf_s("%d", x[i]);
    }

    for (i = 1; i <= 10; i+2)
    {
        if (x[i] > x[i + 1])
            max = x[i];
        else
            max = x[i + 1];
    }

    for (i = 1; i <= 10; i + 2)
    {
        if (x[i] < x[i + 1])
            min = x[i];

        else
            min = x[i + 1];
    }

    printf("max = %d , min = %d", max, min);
    return 0;
}
2
  • Welcome to Stack Overflow! (Please read the tour to familiarize yourself with the basics, and maybe browse the Help.) It seems you are having problems formatting your question. Did you click the ? button for a short version of Formatting Help? If it is not enough, read How do I format my posts using Markdown or HTML?. It mentions not to use `code ticks` for multi-line code, but to use the {} button (or Ctrl+K) instead. Commented Dec 24, 2018 at 2:00
  • 1
    .. In addition, please add how your code does not work. Commented Dec 24, 2018 at 2:01

4 Answers 4

3

In C, arrays are declared with their number of elements, such as int x[10];, as you did. Though, you access its elements starting on x[0]. Since there are 10 elements, the last element is x[9].

So your for loop should look like this:

    for(i=0; i<10; i++)

Also, scanf_s() requires the address to put the data on. If you pass x[i] to scanf_s(), you're passing an int, though it requires int * (a pointer to int).

        scanf_s("%d", &x[i]);

Going further, the third expression in the for loop is an instruction it's going to execute as soon as the control reaches the end of the for block. Maybe i+2 was a typo, but you should replace it with i++, to increment the value of i at each iteration.

Finally, you're only comparing adjacent elements in the array. That doesn't give you the min and max elements. You should first initialize those variables to the first element in the array (x[0]), and then loop the rest of the array, starting with i=1, and updating the values of min and max only if the current element x[i] is lower or higher, respectively, than min and max.

Putting everything together:

#include <stdio.h>

int main()
{
    int i, max, min, x[10];

    for(i=0; i<10; i++)
    {
        printf("Please enter a number: ");
        scanf_s("%d", &x[i]);
        printf("You entered %d\n", x[i]);
    }

    min = max = x[0];

    for(i=1; i<10; i++)
    {
        if(x[i] < min)
            min = x[i];
        if(x[i] > max)
            max = x[i];
    }

    printf("min = %d, max = %d\n", min, max);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

First, array index starts from 0 in C. So when you declare int x[10], and you want to traverse this array, you have to do like below

for (i = 0; i < 10; i++)
{
    x[i];
}

It seems the scanf_s went wrong, though I don't know how scanf_s is declared. If it works the same as scanf, it could be

for (i = 0; i < 10; i++)
{
    printf("please enter a number: ");
    scanf("%d", &x[i]);
}

Finally, your algorithms of finding the maximum element and the minimum element don't work as your expected. It should be

for (max = x[0], i = 1; i < 10; i++)
{
    if (x[i] > max)
        max = x[i];
}

for (min = x[0], i = 1; i < 10; i++)
{
    if (x[i] < min)
        min = x[i];
}

Comments

0
  1. scraf must pass ref.
  2. max and min can write more elegant.

    int main() {

    int i = 1;
    int max, min;
    int x[10];
    
    for (i = 0; i < 10; i++) {
        printf("please enter a number: ");
        scanf("%d", &x[i]);
        printf("%d\n", x[i]);
    }
    
    min = x[0];
    max = x[0];
    for (i = 1; i < 10; i++) {
        if (max < x[i]) {
            max = x[i];
        }
    
        if (min > x[i]) {
            min = x[i];
        }
    }
    
    printf("max = %d , min = %d", max, min);
    return 0;
    

    }

1 Comment

@chux change i from 0 to 9
0

In simple words, You have declared an array of 10 elements but your for loop is only iterating for 9 times. This is because your for loop runs from x=1 to x<=10 i.e, the loop will run and accept elements when x values are 1,2,3,...8,9 and hence only 9 times(1 less than what you want). You have two solutions for this 1) declare x ranges from x=1 to x=10 But since array starts with 0, better solution will be, 2) declare x ranges from x=0 to x<= 10 Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.