How can I replace an element in array using scanf function ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • binarylife
    New Member
    • Oct 2010
    • 4

    #1

    How can I replace an element in array using scanf function ?

    Hi all,

    First I want the user to input values in 10 numbered-array
    If any number is less than 50 , the user must replace it by scanf.

    This is what I have for now :
    Code:
    #include <stdio.h>
    #define N 5
    
    int main()
    {
        int array[N];
        int i,element;
    
    
        printf("Enter the array:>");
        for (i=0;i<N;i++) { 
        scanf("%d",&array[i]);
        if (array[i]<=50){
        element=array[i];
        scanf("%d",&element);
        }
        }
        printf("Your Array is:>\n");
        for (i=0;i<N;i++)
        printf("%d\n",array[i]);
    
    
    
    
        return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I would use two loops. The first loop allows the user to enter the integers. Then the second loop makes a pass over the array and has the user re-enter any incorrect values. The new values can be our right in the array as you have done in your code.

    Comment

    • binarylife
      New Member
      • Oct 2010
      • 4

      #3
      That's my problem , I can't figure out how to pass over the array and then re-enter the value
      thanks !

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You have aready done a pass over the array to put in your initial values.

        Make a copy of your current loop and remove the first scanf.

        Comment

        Working...