CP Lab 14
CP Lab 14
OBJECTIVE:
This lab will introduce loops available in C Language Program. At the end of this lab, you should be able to:
APPARATUS:
• Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10
ARRAYS:
Recall the task we did in one lab for entering marks of five subjects and calculating the average of those. Firstly, we did
it by declaring 5 variables and later we did it using a for loop and summing up all values. What if you want to store
the marks of the whole class comprising of 50 students? Declaring 50 variables for this would not be a good solution.
Secondly, while using a for loop even for the case of 5 subject marks, the individual marks were never saved, we just
kept adding entered marks and could not access the individual entries.
The solution for such cases is the use of Array which is defined as single variable but can hold multiple values inside it.
The entries of array are generally known as elements or items.
Arrays are a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array
is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same
type.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of elements required
by an array as follows:
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and
data_type can be any valid C data type. For example, to declare a 5-element array called age of type int,
use this statement:
Initializing Arrays
int age[5] = {23, 25, 24, 21, 26}; // declaration and initialization of an array
The number of values between braces { } cannot be larger than the number of elements that we declare for
the array between square brackets [ ]. If you omit the size of the array, an array just big enough to hold the
initialization is created. Therefore, if you write:
int age[] = {23, 25, 24, 21, 26}; // same array as previously
You will create the same array as you did in the previous example.
Once an array is defined, we can access the individual elements from the array using the index of the element.
All arrays have 0 as the index of their first element which is also called the base index and the last index of
an array will be total size of the array minus 1. Shown below is the pictorial representation of the array we
discussed above:
Index 0 1 2 3 4
age 23 25 24 21 26
We can access any element of the array using the format array[index], for example, age[2] will access
the element at index 2 which is the third element with value 24. For example:
The above statement will take the element at index 4, which is the 5th element from the array age and assign
the value to myAge variable.
We can reassign the element of the array using the same notation. Following is an example to reassign a single
element of the array:
The above statement assigns the 5th element in the array with a value of 28.
As we know we can access any element of the array using the index of the element and index starts with 0 and
of course, the last index is 1 less than total elements in the array.
We can use a for loop to generate the indices of the array and then using the array[index] notation, we
can print those elements.
#include <stdio.h>
int main() {
int age[] = {23, 25, 24, 21, 26};
for (int i=0;i<5;i++){
printf("%d\t",age[i]);
}
return 0;
}
On a similar way, to populate an array from user input integers, we can use a for loop and inside it we can
take input from user and assign that to array using the same array[index] notation.
#include <stdio.h>
int main() {
int age[5];
In the above case we assumed that user wants to enter 5 values every time. We can also first ask the user
about the number of values he/she wants to enter and then create the array of that size. The code is here:
#include <stdio.h>
int main() {
int size;
printf("How many values you want to store:");
scanf("%d",&size);
int age[size];
After taking the input from user, relate that to element of the above
array and print the result.
Enter values1:23.4
Enter values2:12.5
Enter values3:6.77
Enter values4:8.907
Enter values5:4.556
Enter values6:3
Sample Output 1 Enter values7:2
Enter values8:4.5
Enter values9:3.8
Enter values10:1.2
We can process the complete array for different calculations by accessing each element inside the array using a for
loop. Below is an example to calculate the sum and the product of all numbers inside array.
#include <stdio.h>
int main() {
float values[5]={2.3,6.3,7,8.5,9};
float sum=0,product=1;
for (int i=0;i<5;i++){
sum+=values[i];
product*=values[i];
}
TASK 14.4: Find how many Senior Person ages inside an array
Define an array of size 10 which has the age of 10 persons like:
int age[10]={23,43,56,74,12,13,27,63,49,18};
You can change the values as per your desire. The task is to display how
many Senior Persons are there in the array where a person is considered
Senior if the age is 50 or above.
Change different values inside the array and test the output.
int numbers[10]={23,43,56,74,12,13,27,63,49,18};
You can change the values as per your desire. The task is to display how
many are Prime Numbers inside the array. You must have the isPrime
function we created so many times.
Change different values inside the array and test the output.
• Average Marks
• No. of Failing Students
• Highest Marks obtained along with the roll number.
• Lowest Marks obtained along with the roll number.
***************************************************
* Result of Computer Programming_I *
***************************************************
* Average Marks: 60.2 *
* Failing Students: 1 *
* Highest Marks:81 Obtained by Roll No. :3 *
* Lowest Marks: 32 Obtained by Roll No. :4 *
***************************************************
• Write the logic to assign values to array pair such that the sum
of first two values of array data goes to first location of pair,
then sum of third and fourth location of array data goes to second
location of pair. And so on, as shown in figure below.
(Hint: Write a loop with variable i, take values from data[i] and
data[i+1], add them and copy it to pair)
For this you can define an array as number of days in each month:
Take in the day and month from the user and then relate the month number
and the array index to find and display the next date.