You are here:

C/need clarity

Advertisement


Question
Hello smitha,

please, there something i need you to help me out with . here is a programme i have written but need a little help on finishing it:

#include<stdio.h>

int main()
{

int arr[6]= {0}; //initialising the array
int num;
int num_Entered;

printf(how many number do u want to enter?);
scanf("%d", &num);

for(i=0;i<num;i++){
printf("enter the %d number",i); //here is where i want to modify
scanf("%d", &num_Entered);
}

printf("numbered entered are :");
for(i=0;i<num;i++)
printf("%d", arr[i]);

my questions when u run the programme, and suppose the user decides to enter 4 numbers, that means in the for loop i want to see sentences like this :
programme run:

how many number do you want to enter?
4
enter the first number
1
enter the second number
2
enter the third number
3
enter the fourth number
4
how do i write the prinf() in the for loop to display the information like this ?


Answer
Dear Henry

I'm really sorry for the delay in responding to your query.

The following is the corrected code:


#include<stdio.h>

int main()
{

int arr[6]= {0}; //initialising the array
int num;
int num_Entered;

printf(how many number do u want to enter?);
scanf("%d", &num);

for(i=0;i<num;i++){
printf("enter the %d number",i);
scanf("%d", &arr[i]);  // this is where I have made the change
}

printf("numbered entered are :");
for(i=0;i<num;i++)
printf("%d", arr[i]);
}

Before I explain the changes I have made, I'll give you a brief about arrays and the scanf() function in C:
-> In C the name of the array denotes the address in the memory where it is allocated. So in this case the address of your array 'arr' is also denoted by 'arr'.
-> The scanf() function requires, as its second argument, the address of the location in memory to which the entered value has to be stored. This is the reason we give an ampersand ('&') sign before the variable in scanf(). But in our case the name of the variable is that of an array and also, as I discussed above, the name of the array denotes its address in memory. Then why are we still using '&'? The reason is :
Although the name of the array is its address also, please remember that it is actually the address of its first subscript. The how do we provide scanf() with the successive addresses with each iteration of the for loop? Here lies the reason for providing the construct:
scanf("%d",&arr[i]);

With the above each time the loop iterates, the address of the latest subscript in question will be provided to the scanf() function.

Hope this satisfies your query.
Warm Regards
Smitha Renny

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Smitha Renny

Expertise

I can answer questions related to basic understanding of C concepts, arrays, pointers and their handling well.

Experience

I've been programming in BASIC since 1987. Later moved onto Pascal for a short period and then hooked onto C since 1997.

Education/Credentials
I have completed a Post-Graduate diploma in Computer Applications. Currently doing MCA

©2012 About.com, a part of The New York Times Company. All rights reserved.