By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

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

P: 4
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 :
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define N 5
  3.  
  4. int main()
  5. {
  6.     int array[N];
  7.     int i,element;
  8.  
  9.  
  10.     printf("Enter the array:>");
  11.     for (i=0;i<N;i++) { 
  12.     scanf("%d",&array[i]);
  13.     if (array[i]<=50){
  14.     element=array[i];
  15.     scanf("%d",&element);
  16.     }
  17.     }
  18.     printf("Your Array is:>\n");
  19.     for (i=0;i<N;i++)
  20.     printf("%d\n",array[i]);
  21.  
  22.  
  23.  
  24.  
  25.     return 0;
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
Dec 19 '10 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Dec 19 '10 #2

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

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Dec 19 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.