C Program Learning Part-8
DECISION MAKING AND LOOPING
Given a number, write a program using loop to reverse the digits of the number. For example, the number 12345 Should be written as 54321
#include
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer:
");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number =
%d", rev);
return 0;
}
Write a program to find maximum number N input numbers?
#include
int main()
{
int i,num,n,large=0, loc=0;
printf("How many numbers:
");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("\nEnter number %d:
",i);
scanf("%d",&num);
if(num>large)
{
large=num;
loc=i;
}
}
printf("\n\nThe Largest Number is %d %d
",large,loc);
return 0;
}
Arrays:
An Array is a fixed-size sequenced collection of elements of
the same data type. It is simply a grouping of like-type data. In its simplest
form, an array can be used to represent a list of numbers, or a list of names.
For example, we can use an array name salary to represent a set of salaries of
a group of employees in an organization. For example:
salary [10]
represents the salary of 10th employee. While the complete
set of values is referred to as an array, individual values are called
elements.
One Dimensional Arrays:
A list of items can be given one variable name only one
subscript and a variable is called a single-subscripted variable or a one
dimensional array. For example, if we want to represent a set of five numbers,
say (35, 40, 20, 57, 19), by an array variable number, then we may declare the
variable number as follows
int number[5];
and the computer reserves the five storage locations as
follows below:
Declaration of one Dimensional Arrays:
Like any other variable, arrays must be declared before they
are used so that the complier can allocate space for them in memory. The
general form of array declaration is
type variable-name[size];
The type specifies the type of element that will be
contained in the array, such as int, float, char and the size indicates the
maximum number of elements that can be stored inside inside the array. For
example,
float height[50];
Why we need to allow one extra element space when declaring
character arrays?
The size in a character string represents the maximum number
of characters that that the string can hold. For instance,
char name[10];
declares the name as a character array variables that can
hold a maximum of 10 characters. Suppose we read the following string constant
into the string variable name.
“WELL DONE”
Each character of the string is treated as an element of the
array name and is stored in the memory as follows: When the compiler sees a
character string, it terminates it with an additional null character. Thus, the
element name[10] holds the null character ‘\0’.
Initialization of one dimensional arrays:
After an array is declared, its elements must be
initialized. Other wise they contain ” garbage ”. An array can be initialized at
either of the following stages:
1. At compile time
2. At runtime
Compile time initialization:
The general form of initialization of arrays is:
type array-name[size] = {list of values};
The values in the list are separated by commas. For instance, the
statement
int number[3] = { 0, 0, 0};
will variable number as
an array of size 3 and will assign zero to each element.
Run time initialization:
An array can be explicitly initialized at run time. For
example consider the following segment of the C program.
for( i = 0; i < 100; i = i + 1)
{
if( i < 50)
sum[i] = 0.0;
else
sum[i]=1.0;
}
The first 50 elements of the array sum are initialized to
zero while the remaining 50 elements are initialized to 1.0 at run time.
We can also use a read function such as scanf to initialize
an array.
For example, the statements
int x[3];
scanf(“%d%d%d”,&x[0],
&x[1], &x[2]);
Two Dimensional Arrays:
Consider The table:
#include
main()