You are on page 1of 45

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Compute the average marks
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.

Dr. Yousaf, PIEAS


Compute the average marks
Suppose there are 3 students in a class. Compute
the average marks of these students in a test.
#include<stdio.h>
For the solution, what we int main ()
need? {
3 variables to store three float marks1, marks2, marks3;
values of the marks. float avg;
Name these as marks1, marks1 = 34.5;
marks2, marks3. marks2 = 56;
Add all these values. marks3 = 83.5;
Divide this sum by 3.0. avg =
The average value may (marks1+marks2+marks3)/3.0;
contain decimal point. printf("The average marks are:
%f", avg);
getchar(); return 0;}
Dr. Yousaf, PIEAS
Compute the average marks
Suppose there are 10 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.

Dr. Yousaf, PIEAS


Compute the average marks
Suppose there are 10 students in a class. Compute the
average marks of these students in a test.
#include<stdio.h> marks7 = 57.3;
int main ()
marks8 = 33.6;
{
float marks1, marks2, marks9 = 66.2;
marks3, marks4, marks10 = 85.8;
marks5,marks6, marks7, avg = (marks1 + marks2 +
marks8, marks9, marks10;
marks3 + marks4 + marks5 +
float avg; marks6 + marks7 + marks8
marks1 = 56.5; +marks9 + marks10)/10.0;
marks2 = 78.0;
marks3 = 23.2;
marks4 = 89.1; printf("The average marks
marks5 = 43.7; are: %f", avg);
marks6 = 91.6; getchar();return 0; }
Dr. Yousaf, PIEAS
Compute the average marks
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).

Arrays
Dr. Yousaf, PIEAS
Compute the average marks
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


Dr. Yousaf, PIEAS
Compute the average marks
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?

Solution is: Arrays


Dr. Yousaf, PIEAS
Compute the average marks
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?

Solution is: We can handle it using just ONE


variable. Arrays

Dr. Yousaf, PIEAS


Arrays

Dr. Yousaf, PIEAS


Introduction to Arrays
int marks = 70; // single value

int marks[6]={36,78,29,36,7,99};

Dr. Yousaf, PIEAS


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
Arrays
#include<stdio.h>
int main()
{
int marks[6]={36,78,29,89,7,99}; // array
printf("Marks are %d\n",marks); // ???
getchar();
return 0;
}

Dr. Yousaf, PIEAS


How to Print Arrays?
#include<stdio.h>
int main()
{
int marks[6]={36,78,29,89,7,99};
int i;
for (i = 0; i <6;i++)
printf("Marks are %d\n",marks[i]);

getchar();
return 0;
}
Dr. Yousaf, PIEAS
How to read values of Arrays?
#include<stdio.h>
int main()
{
int marks[6];
int i;
for (i = 0; i <6;i++)
{
printf(“Please enter the marks of students\n”);
scanf(“ %d",&marks[i]);
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
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
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


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 = 0.0;
for (month=0; month < 12; month++)
{
total = total + expenses[month];
}
for (count=0; count < 12; count++)
printf ("Month %d = %.2f Rupees\n", count+1, expenses[count]);
printf("Total = %.2f Rupees\n Average = %.2f Rupees\n", total,
total/12.0);
getchar(); return 0; } Dr. Yousaf, PIEAS
Arrays Name of array (Note
that all elements of this
• Array array have the same
– Structures of related data items name, c)
– Group of consecutive memory locations
– Same name and type c[0]
• To refer to an element, specify c[1]
c[2]
– Array name c[3]
– Position number c[4]

• Format:
c[5]
c[6]
arrayname[ position number ] c[7]
– First element at position 0 c[8]
– n element array named c:
c[9]
c[10]
• c[ 0 ], c[ 1 ]...c[ n – 1 ] c[11]

Position number of
the element within
Dr. Yousaf, PIEAS
array c
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


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
Using Constants to Define Arrays
It is useful to define arrays using #define:
#define MONTHS 12
int array [MONTHS];

Dr. Yousaf, PIEAS


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


If not enough initializers, rightmost
elements become 0

#include<stdio.h>
int main()
{
int marks[6]={36,78};
int i;
for (i = 0; i <6;i++)
printf(“%d\n",marks[i]);

getchar();
return 0;
} Dr. Yousaf, PIEAS
Important Points
//C arrays have no bounds checking
#include<stdio.h>
int main()
{
int marks[6]={10,20,30,40,50,60};
int i;
for (i = 0; i <8;i++)
printf(“%d\n",marks[i]);

getchar();
return 0;
}
Dr. Yousaf, PIEAS
Important Points
If more values are provided than the size of the array as:
#include<stdio.h>
int main()
{ This program
int marks[6]={10,20,30,40,50,60,70,80}; will give error
int i; and will not
for (i = 0; i <8;i++)
run.
printf(“%d\n",marks[i]);
Error: Too many
getchar(); intializers
return 0;
}

Dr. Yousaf, PIEAS


Important Points
#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 students\n");
scanf("%d",&marks[i]); // suppose we enter 1 and 2.
}
for (i = 0; i <6;i++)
printf(“%d\n",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

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).

Dr. Yousaf, PIEAS


Important Points
#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 students\n");
scanf("%d",&marks[i]);
}
for (i = 0; i <6;i++)
printf(“%d\n",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 occurred”. It will notPIEAS
Dr. Yousaf, show any output.
Some Examples of Arrays

Dr. Yousaf, PIEAS


Summation of Marks
#include<stdio.h>
int main()
{
int marks[6]={36,78,7,99,43,29};
int i, sum;
sum = 0;
/* Complete It */
printf("Sum of Marks are %d\n", sum);
getchar(); return 0; }

Dr. Yousaf, PIEAS


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 %d\n", sum);
getchar(); return 0; }
Dr. Yousaf, PIEAS
Summation of Marks
#include<stdio.h>
int main()
{
int marks[6]={36,78,7,99,43,29};
int i, sum;
sum = 0;
i = 0;
while (i <6)
{
sum = sum + marks[i];
i++;
}
printf("Sum of Marks are %d\n", sum);
getchar(); return 0; }

Dr. Yousaf, PIEAS


Summation of Marks
#include<stdio.h> do
{
int main()
sum = sum + marks[i];
{
i++;
int marks[6] = }
{36,78,7,99,43,29}; while (i <6);
int i, sum; printf("Sum of Marks are
sum = 0; %d\n",sum);
i = 0; getchar();
return 0;
}

Dr. Yousaf, PIEAS


To Find the Maximum Marks
#include<stdio.h>
int main()
{
int marks[6] =
{36,78,7,99,43,29};
int i, Maximum;

/* Complete It */

Dr. Yousaf, PIEAS


To Find the Maximum Marks
#include<stdio.h> Maximum = marks[0];
for (i = 1; i <6;i++)
int main()
{
{ if (Maximum <marks[i])
int marks[6] = {
{36,78,7,99,43,29}; Maximum = marks[i];
int i, Maximum; }
}
printf("Maximum Marks are
%d\n",Maximum);
getchar(); return 0; }

Dr. Yousaf, PIEAS


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


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
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
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


Revision of Arrays Name of array (Note
that all elements of this
• Array array have the same
– Structures of related data items name, c)
– Group of consecutive memory locations
– Same name and type c[0]
– e.g int c[12]; c[1]
c[2]
• To refer to an element, specify c[3]
– Array name c[4]
– Position number
c[5]
c[6]
• Format: c[7]
arrayname[ position number ] c[8]
– First element at position 0
c[9]
c[10]
– n element array named c: c[11]
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Position number of
the element within
Dr. Yousaf, PIEAS
array c
Revisions of Arrays
How to declare array?
typename variablename[size]
int marks[6]={36,78,29,36,7,99};
To print an array, we need loop.
for (i = 0; i <6;i++)
printf("Marks are %d\n",marks[i]);
To take the elements of arrays from a user (scanf),
we need loop.
for (i = 0; i <6;i++)
{
printf(“Please enter the marks of students\n”);
scanf(“ %d",&marks[i]);
}
Dr. Yousaf, PIEAS
Revisions of Arrays
Int a[4] = {2, 4, 3, 10};
We can use a[0]=10;
x=a[2];
a[3]=a[2]; etc.
printf( "%d", a[ 0 ] );
We can declare more than one array
in single line as:
int b[ 100 ], x[ 27 ];
If not enough initializers, rightmost elements
become 0
int n[ 5 ] = { 1 } // All other elements would be 0
C arrays have no bounds checking
Dr. Yousaf, PIEAS
How to store and print a single value
#include <stdio.h>
int main( )
{
int x;

x = 5;

printf(“%d", x);

getchar();
return 0;
}

Dr. Yousaf, PIEAS


How to Store and Print an Array?
#include<stdio.h>
int main()
{
int marks[6]={36,78,29,89,7,99};
int i;
for (i = 0; i <6;i++)
printf("Marks are %d\n",marks[i]);

getchar();
return 0; }

Dr. Yousaf, PIEAS

You might also like