-->

Using arrays in C Programming

Using arrays in C Programming
Using arrays in C Programming
Array elements can be referenced through an index. E.g. a[0], a[4] etc. Index always starts from zero which means to refer to the first element we use 0 as an index.
An array indexer takes index as parameter.
The first element has index 0 and the last element has index length-1.
Array elements can be retrieved and changed by the [ ] operator.

For example:
Using arrays in C Programming,Arrays in C Programming,types of arrays,how to define array,how to declare array,array types,what is array,array concept,use of array,array definition, definition of array, define array,def array,Arrays,Introduction to Arrays in C Programming,Introduction to Arrays in C language,arrays introduction,define array,what is array,use of arrays,Explain the concept of arrays,Use arrays in C programming,Explain the concept of multidimensional arrays,Explain the concept of matrices two dimensional arrays,Arrays-Example,examples of arrays,cse study zone, Programming for Problem Solving lecture notes, Programming for Problem Solving notes unit wise jntuh,jntuh b.tech r20  Programming for Problem Solving notes,jntuh r20  Programming for Problem Solving class room notes unit wise

Printing Data in an Array
Once you are able to store the values inside an array, you must be eager to know how to print them. Well, it is very simple. Just use the kind of loop that you used while storing the values in an array.
The following program stores the salary figures of the 12 months and prints out the average monthly salary.

#include<stdio.h>
main()
{
int salary[12], count, sum=0;
float average;
/*Entering data into array */
for(count=0; count<12; count++)
{
printf(“Enter salary figure for month %d:”,count+1);
scanf(“%d”,&salary[count]);
}
//this loop executes for 12 times. During each iteration, you will be promted with the message. “enter salaray figure month.” And the scanf() statement accepts the value you enter and puts it in the array at the appropriate location.
/*Finding the sum of the 12 salary figures */
for(count=0; count<12; count++)
{
Sum=sum+salary[count];
}
/*this loop executes for 12 times. During each iteration, the salary figure stored in the appropriate location in the array gets added to the variable sum and the result is stored back in the variable sum. By the end of the 12th iteration, the variable sum contains the total of all salary figures  stored in the array.*/
average=(float) sum/12; 
/*here you calculate the average total salary for the 12 months. Hence, the average contain decimal points, we need the type cast variable sum as float data type.
printf(“Average total salary during 12 months = %f”, average);
} */

OUTPUT
Enter salary figure for month 1: 1000
Enter salary figure for month 2: 2000
Enter salary figure for month 3: 3000
Enter salary figure for month 4: 4000
Enter salary figure for month 5: 5000
Enter salary figure for month 6: 6000
Enter salary figure for month 7: 7000
Enter salary figure for month 8: 8000
Enter salary figure for month 9: 9000
Enter salary figure for month 10: 10000
Enter salary figure for month 12: 11000
Enter salary figure for month 12: 12000
Average total salary during 12 months = 6500.00

Example: Program to print data in an array
/*Initialize array with square of index and print it. */
#include<stdio.h>
#includ<coniio.h>
#define ARY_SIZE 5
int main(void)
{
// Local Declarations
int sqrAry[ARY_SIZE]; //the integer value is declared
// statements
for(int i=0; i<ARY_SIZE; i++) /*’i' is initialized to ‘0’ before the loop. ‘i' is checked for less than ARY_SIZE at the beginning of loop and ‘I' is incremented by ‘1’ at end of the loop*/
sqrAry[i] = i*i;
printf(“Elements\tSquare\n”); /*the heading is printed*/
printf(“====\t====\n”);
for(int i=0; i<ARY_SIZE; i++) /* when ‘i' is zero and less than ARY_SIZE, the value of ‘I' is incremented*/
printf(“%5d\t%4d\n”,i, sqrAry[i]); /*the value of sqrAry is printed in the output.*/
return 0;
// main

OUTPUT
Element Square
-------------------------------
0 0
1 1
2 4
3 9
4 16

// hence the array size is 5, the values taken will be from 0 to 4. The square of the values will be shown in the output.

Example:
/* Read a number series and print it reversed */

#include<stdio.h>
int main(void)
{
// Local Declarations
int readNum; /*readNum and numbers are declared as integers. The array size of numbers is taken as 50. */
int numbers[5];
// Statements
printf(“You may enter up to 50 integers:\n”); /*Text displays asking user to enter the integers up to 50*/
printf(“How many would you like to enter?”); /* Text displays asking user to enter How many would you like to enter.*/
scanf(“%d”, &readNum); //readNum is scanned.
if(readNum > 50) /*if the readNum is greater than or equal to 50, the text displays asking user to enter numbers.*/
readNum=50;
//Fill the array
Printf("\n Enter your numbers: \n”);
for(int i = readNum; i++)
scanf(“%d”, &numbers[i]
// Print the array
printf(“\n Your numbers reversed are: \n”);
for( int i= readNum-1, numPrinted =0;  i>= 0; i--) /*’i' is initialized to ‘readNum-1’, numPrinted is ‘0’ before the loop. ‘i' is checked for greater than or equal to ‘0’ at the beginning of loop and ‘i' is decremented by ‘1’ at end of the loop.*/
{
printf(“%3d”, numbersfill);
if(numPrinted < 9)
numPrinted++; //the numbers are printed in output.
else
{
printf(“\n”;
numPrinted = 0; /*if numPrinted is less than 9, numPrinted is incremented else numPrinted will be zero.*/
} //else
} //for
Return 0;
} // main

OUTPUT
You may enter up to 50 integers:
How many would you like to enter? 12
Enter your numbers:
1 2 3 4 5 6 7 8 9 10 11 12

Your numbers reversed are:
12 11 10 9 8 7 6 5 4 3 2 1

Related Posts

Subscribe Our Newsletter