Program in C using one-dimensional array that determines the lowest value among five input values from the keyboard and prints the difference of each value from the lowest.
INPUT: 28643
OUTPUT:
5
4
2
1
#include %26lt;stdio.h%26gt;
main()
{
int inp[5],i,min;
printf("Enter 5 values--%26gt;");
for (i=0; i%26lt;5;i++)
{
scanf("%d", %26amp;inp[i]);
}
min=inp[0];
for (i=1; i%26lt;5; i++)
{
if (inp[i] %26lt; min)
min=inp[i];
}
printf("Minimm=%d\n",min);
for(i=0; i%26lt;5;i++)
{
printf("%d\n", inp[i]-min);
}
}
Can you help me to explain this program in C (above) sentence-to-sentence. I need to explain this ASAP as part of my major requirement in my subject. pls.!!
Hey!!!Are you a programmer ?? Can you help me to explain this Program in C-language? (below)? please !!?
Here's the explanation:
-------------------------
#include %26lt;stdio.h%26gt;
-------------------------
This line tells us to INCLUDE a header file containing standard input output functions in our program, so that we can get input from user or output something to screen.
Then the main( ) function starts, where actual processing of the program is done.
---------------------
int inp[5],i,min;
---------------------
This line declares three integer type variables, the first one is an array (of 5 elements) for taking input numbers, the second variable is just used for loop iterations or as index value, and the third variable is used to store the minimum value.
--------------------------------------
printf("Enter 5 values--%26gt;");
--------------------------------------
This lines uses a function printf( ), which is used to print the argument, provided to this function, to screen. So this will print this text "Enter 5 values--%26gt;".
-----------------------------
for (i=0; i%26lt;5;i++)
{
scanf("%d", %26amp;inp[i]);
}
-----------------------------
This loop starts with the value of i=0 upto i=4, means it will repeat itself 5 times. In each run, it calls a function scanf( ), to scan or get the input of user.
First argument to this function, "%d", specifies that the input is going to be an integer, and the second argument is the address of the variable where this number should be stored. "%26amp;" operator is used to get the address. "inp[i]" indicates the ith element of the array inp. i.e. if i is 2, it means inp[2], which in turn means, the 3rd element of the array inp.
In this way, the loop gets all 5 numbers into the array inp.
---------------
min=inp[0];
--------------
This line assumes that inp[0], the first element of inp, is the smallest number. Note, this is just assumption. In actual, it may not be the smallest number.
------------------------
for (i=1; i%26lt;5; i++)
{
if (inp[i] %26lt; min)
min=inp[i];
}
------------------------
Again a loop that runs from 0 to 4, is used to get the actual minimum number. This is acheived by comparing the minimum number stored in "min" variable by each element in array "inp". If, in iteration, "min" is found to be less than the i-th element of inp array, it means that our previously stored minimum number is not the minimum, rather the current i-th element of inp array is minimum. So we assign that i-th element's value to the minimum number. Like if we had 22 in the "min" variable, and in the array, at i=2, the inp[2] had the value 16, then we should replace the value 22 in "min" by 16, so that now minimum number should reflect the correct value.
This process repeats in the loop until the loop ends.
-----------------------------------
printf("Minimm=%d\n",min);
-----------------------------------
Again, printf is used to output the text specified in arguments. %d means it should display a number inplace of %d. And this number comes from the "min" variable, the 2nd argument.
------------------------------
for(i=0; i%26lt;5;i++)
{
printf("%d\n", inp[i]-min);
}
------------------------------
In this loop, the difference of the each array element and the minimum number is printed. "\n" character is used to print a newline character, i.e. next line.
Reply:%26lt;stdio.h%26gt; is a header file,
main() is a function, a function returns a value.
int inp[5],i,min; is a declaration (all of the declaration is integer)
printf("Enter 5 values--%26gt;"); is the code for the "Enter 5 values--%26gt;" to appear in the execution.
for (i=0; i%26lt;5;i++) is a loop it has 3 parts, the intialization i=0; Comparison i%26lt;5; and increase i++. it is done when you need an iteration in ur program.
scanf("%d", %26amp;inp[i]); this is a code for the user to type a value during the execution.
if (inp[i] %26lt; min)
min=inp[i]; an if statement that input is less than the minimum and if the situation is true the value of min=inp[i];
I think that the rest are repition of the codes. I hope it helps you an some ways.
survey for money
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment