2. Compute the average marks
Dr. Yousaf, PIEAS
Suppose there are 3 students in a class. Compute
the average marks of these students in a test.
For the solution, what we
need?
3 variables to store three
values of the marks.
Name these as marks1,
marks2, marks3.
Add all these values.
Divide this sum by 3.0.
The average value may
contain decimal point.
3. Compute the average marks
Dr. Yousaf, PIEAS
Suppose there are 3 students in a class. Compute
the average marks of these students in a test.
For the solution, what we
need?
3 variables to store three
values of the marks.
Name these as marks1,
marks2, marks3.
Add all these values.
Divide this sum by 3.0.
The average value may
contain decimal point.
#include<stdio.h>
int main ()
{
float marks1, marks2, marks3;
float avg;
marks1 = 34.5;
marks2 = 56;
marks3 = 83.5;
avg =
(marks1+marks2+marks3)/3.0;
printf("The average marks are:
%f", avg);
getchar(); return 0;}
4. Compute the average marks
Dr. Yousaf, PIEAS
Suppose there are ten students in a class. Compute
the average marks of these students in a test.
For the solution, what we need?
10 variables to store ten values of the
marks. Name these as marks1, marks2, …
marks 10.
Add all these values. Divide this sum by
10. The average value may contain
decimal point.
5. Compute the average marks
Dr. Yousaf, PIEAS
#include<stdio.h>
int main ()
{
float marks1, marks2,
marks3, marks4,
marks5,marks6, marks7,
marks8, marks9, marks10;
float avg;
marks1 = 56.5;
marks2 = 78.0;
marks3 = 23.2;
marks4 = 89.1;
marks5 = 43.7;
marks6 = 91.6;
marks7 = 57.3;
marks8 = 33.6;
marks9 = 66.2;
marks10 = 85.8;
avg = (marks1 + marks2 +
marks3 + marks4 + marks5 +
marks6 + marks7 + marks8
+marks9 + marks10)/10.0;
printf("The average marks
are: %f", avg);
getchar();return 0; }
Suppose there are ten students in a class. Compute
the average marks of these students in a test.
6. Compute the average marks
Dr. Yousaf, PIEAS
What’s about if there are 30 students?
30 variables would be needed to store values of the
marks. marks1, marks2, … marks 30.
What’s about if there are 100 (or 500) students?
100 (or 500) variables would be needed to store
values of the marks. marks1, marks2, … marks 100
(or 500).
Is it practical approach? Can we handle it using just
ONE variable?
Solution is: Arrays
7. Compute the average marks
Dr. Yousaf, PIEAS
What’s about if there are 30 students?
30 variables would be needed to store values of the
marks. marks1, marks2, … marks 30.
What’s about if there are 100 (or 500) students?
100 (or 500) variables would be needed to store
values of the marks. marks1, marks2, … marks 100
(or 500).
Is it practical approach? Can we handle it using just
ONE variable?
Solution is: Arrays
9. Introduction to Arrays
int marks = 70; // single value
int marks[6]={36,78,29,36,7,99};
Dr. Yousaf, PIEAS
10. Introduction to Arrays
#include<stdio.h>
int main()
{
int marks = 70; // single value
printf("Marks are %d",marks);
/* single value will be printed */
getchar();
return 0;
}
Dr. Yousaf, PIEAS
13. #include<stdio.h>
int main()
{
int marks[6];
int i;
for (i = 0; i <6;i++)
{
printf(“Please enter the marks of studentsn”);
scanf(“ %d",&marks[i]);
}
getchar();
return 0; } Dr. Yousaf, PIEAS
How to read values of Arrays?
14. Single-Dimensional Arrays
Generic declaration:
typename variablename[size]
– typename is any type
– variablename is any legal variable name
– size of array
– For example
int a[10];
– Defines an array of ints with subscripts ranging from 0 to
9
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
– There are 10*sizeof(int) bytes of memory reserved for
this array.
– You can use a[0]=10; x=a[2]; a[3]=a[2]; etc.
Dr. Yousaf, PIEAS
15. Initializing Arrays
• Initialization of arrays can be done by a comma
separated list following its definition.
• For example:
int array [4] = { 100, 200, 300, 400 };
– This is equivalent to:
int array [4];
array[0] = 100;
array[1] = 200;
array[2] = 300;
array[3] = 400;
Dr. Yousaf, PIEAS
16. A Simple Example
#include <stdio.h>
int main()
{
float expenses[12]={10.3, 9, 7.5, 4.3, 10.5, 7.5, 7.5, 8, 9.9, 10.2,
11.5, 7.8};
int count, month;
float total;
for (month=0, total=0.0; month < 12; month++)
{
total+=expenses[month];
}
for (count=0; count < 12; count++)
printf ("Month %d = %.2f Rupeesn", count+1, expenses[count]);
printf("Total = %.2f Rupeesn Average = %.2f Rupeesn", total,
total/12.0);
getchar(); return 0; } Dr. Yousaf, PIEAS
17. Arrays
• Array
– Structures of related data items
– Group of consecutive memory locations
– Same name and type
• To refer to an element, specify
– Array name
– Position number
• Format:
arrayname[ position number ]
– First element at position 0
– n element array named c:
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Name of array (Note
that all elements of this
array have the same
name, c)
Position number of
the element within
array c
c[6]
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Dr. Yousaf, PIEAS
18. Arrays
Array elements are like normal variables
c[0] = 3;
printf( "%d", c[ 0 ] );
– Perform operations inside brackets.
If x = 3, then the following comparisons are same.
c[5 - 2] or c[ 3 ] or c[ x ]
Dr. Yousaf, PIEAS
19. Declaring Arrays
• When declaring arrays, specify
– Type of array
– Name
– Number of elements
arrayType arrayName[numberOfElements];
– Examples:
int c[ 10 ];
float myArray[ 32 ];
• Declaring multiple arrays of same type
– Format similar to regular variables
– Example:
int b[ 100 ], x[ 27 ];
Dr. Yousaf, PIEAS
20. Using Constants to Define Arrays
It is useful to define arrays using #define:
#define MONTHS 12
int array [MONTHS];
Dr. Yousaf, PIEAS
21. Important Points
• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements
become 0
int n[ 5 ] = { 1 }
• All other elements would be 0
(Example on next slide)
Dr. Yousaf, PIEAS
23. Important Points
//C arrays have no bounds checking
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int marks[6]={10,20,30,40,50,60};
int i;
for (i = 0; i <8;i++)
printf(“%dn",marks[i]);
getchar();
return 0;
}
24. Important Points
If more values are provided than the size of the array as:
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int marks[6]={10,20,30,40,50,60,70,80};
int i;
for (i = 0; i <8;i++)
printf(“%dn",marks[i]);
getchar();
return 0;
}
This program
will give error
and will not
run.
Error: Too many
intializers
25. #include<stdio.h>
int main()
{
int marks[6]; // no initialization
int i;
for (i = 0; i <2; i++) //scanning lesser values than the size of array
{
printf("Please enter the marks of studentsn");
scanf("%d",&marks[i]); // suppose we enter 1 and 2.
}
for (i = 0; i <6;i++)
printf(“%dn",marks[i]); // printing 6 values
getchar(); return 0;}
// First two values will be 1 and 2. Rest will be garbage (not
zeroes), as shown in the next slide.
Dr. Yousaf, PIEAS
Important Points
26. Dr. Yousaf, PIEAS
Important Points
So if values (lesser than the size of array) are initialized at
the time of declaration) then rest of the values are set to
zeros, however if values (lesser than the size of array) are
initialized through scanf then rest of the values are
garbage values (not zeros).
27. #include<stdio.h>
int main()
{
int marks[6]; // no initialization
int i;
for (i = 0; i <8; i++) //scanning more values than the size of array
{
printf("Please enter the marks of studentsn");
scanf("%d",&marks[i]);
}
for (i = 0; i <6;i++)
printf(“%dn",marks[i]); // printing 6 values
getchar(); return 0;}
This program will run (no error). It will take 8 values. When
scanning loop will terminate, you will get message: “A buffer
over run has occuered”. It will not show any output.
Dr. Yousaf, PIEAS
Important Points
30. Summation of Marks
#include<stdio.h>
int main()
{
int marks[6]={36,78,7,99,43,29};
int i, sum;
sum = 0;
for (i = 0; i <6;i++)
{
sum = sum + marks[i];
}
printf("Sum of Marks are %dn", sum);
getchar(); return 0; }
Dr. Yousaf, PIEAS
32. #include<stdio.h>
int main()
{
int marks[6] =
{36,78,7,99,43,29};
int i, sum;
sum = 0;
i = 0;
do
{
sum = sum + marks[i];
i++;
}
while (i <6);
printf("Sum of Marks are
%dn",sum);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Summation of Marks
34. #include<stdio.h>
int main()
{
int marks[6] =
{36,78,7,99,43,29};
int i, Maximum;
Maximum = marks[0];
for (i = 1; i <6;i++)
{
if (Maximum <marks[i])
{
Maximum = marks[i];
}
}
printf("Maximum Marks are
%dn",Maximum);
getchar(); return 0; }
Dr. Yousaf, PIEAS
To Find the Maximum Marks
35. The following program declares an array of 5 integers,
initializes the array using an initializer list (complete) and
displays the values stored in array elements
#include<stdio.h>
int main()
{
int A[5]={1,2,3,4,5};
int index=0;
while(index<5)
{
printf(“%d n”,A[index]);
index=index+1;
}
}
Dr. Yousaf, PIEAS
36. Write a complete and an efficient C-Program in which
declare two arrays of 5 integers each, initialize the arrays
from user, and displays the sum of these two arrays.
Write a program that declares two integer arrays of 10
elements each, name the arrays as even and odd. The
program should allow the user to enter 10 numbers, all
even numbers should be stored in array even while all odd
numbers should be stored in array odd, finally the
program should display the contents of the two arrays.
(Yourself)
Dr. Yousaf, PIEAS
37. Write a program that creates an integer array A of 5
elements, initialize the array with a list. The task is to
re‐order this array as specified by user. Ask the user to
enter new order for array elements one by one. For
example if the first number entered by user is 4 for
element 1, this means A[4] should now be the first
element in the array.
Write a program that creates an array of 10 elements,
let the user enter the values, the program should find
the two array elements whose sum is the largest of sum
of any two array elements. For example for an array A
consisting of 20,5,13,7,11 the sum of 20 and 13 is largest
of sum of any two numbers.
(Yourself)
Dr. Yousaf, PIEAS
38. Write program in which declare an array of size 10. Initialize
the elements from the user. Find largest and smallest numbers
in this array. After the smallest and largest numbers are found,
the program should store the largest number at the beginning
of the array and smallest number at the end of the array . For
example if the name of your array is A, A[0] should contain the
largest number and A[N] should contain the smallest number.
Write a program in which declare two arrays A and B for 3
integers each, initialize the arrays from the user. The program
should display the values of these two arrays and later should
exchange the values of A with B i.e. values stored in array A
should be replaced by values stored in array B and values
stored in array B should be replaced by values stored in array A.
It is called swapping. Finally the program should display the
new values of A and B. (Hint , for swapping you might need a
third array ).
(Yourself)
Dr. Yousaf, PIEAS