0% found this document useful (0 votes)
4 views4 pages

Lecture 01 - Array

The document provides an introduction to arrays as a data structure, explaining their characteristics, such as having a name, multiple cells, and specific indices. It includes examples of how to declare, manipulate, and access array elements, along with exercises to create programs that perform various operations on arrays, such as calculating averages and counting elements. Additionally, it outlines solutions to the exercises, demonstrating the use of loops and conditional statements in programming.

Uploaded by

4hs4rygzcr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Lecture 01 - Array

The document provides an introduction to arrays as a data structure, explaining their characteristics, such as having a name, multiple cells, and specific indices. It includes examples of how to declare, manipulate, and access array elements, along with exercises to create programs that perform various operations on arrays, such as calculating averages and counting elements. Additionally, it outlines solutions to the exercises, demonstrating the use of loops and conditional statements in programming.

Uploaded by

4hs4rygzcr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array

Introduction
10
int A = 10; 15
A = 15; A

• Array is a data structure ‫هيكلية تخزين البيانات‬


• Array has a name as variable but it has one or several cells ‫لديها اسم خاص بها مثل المتغير ولكن ايضا لديها مجموعة من الخاليا‬
• Each cell has a specific index (rank) ‫لك خلية رقم خاص بها‬
• The index of the first cell is always zero
• We can manipulate array in sequential or direct order

Example:
Declare an array A with 5 cells of integer

int A[5];

0 1 2 3 4

Remark: To refer to a cell, write the array name then the cell number in between square brackets

A[index]
A
A[2] = 18;
21 18
A[0] = A[2] + 3;
0 1 2 3 4

0 1 2 3 4

A[0] A[1] A[2] A[3] A[4]

To refer to array cells


A[0]
A[1]
A[2]
..
A[4]

The best way to manage the array cells is to use loop

0 1 2 3 4

A[i]
Exercise
Write a program to:
a. Create an array A[10] of integers
b. Fill the array with positive numbers
c. Calculate and print the average of all numbers
d. Count and print the odd elements (numbers)
e. Find and print the largest and smallest element (number)

A
14 10 7 22 6 3 15 44 2 18
0 1 2 3 4 5 6 7 8 9

The output
Average: 14.1
Count of odd: 3
Largest nbr: 44
Smallest nbr: 2

Solution

#include <stdio.h>
void main(){
a) int A[10];
int i, s, c, max, min;
float av;

b) for(i=0; i<10; i++){


do{
printf("Enter a positive nbr:");
scanf("%d", &A[i]);
}
while(A[i] < 0); ‫االرقام السلبية غير مسموحة‬
}

c) s=0;
for(i=0; i<10; i++){
s = s + A[i];
}
av = (float) s/10;
printf("Average: %.1f\n", av);

d) c=0;
for(i=0; i<10; i++){
if(A[i] % 2 != 0)
c = c + 1; //c++
}
printf("Count of odd: %d\n", c);

e) max=min=A[0];
for(i=0; i<10; i++){
if(A[i] > max)
max = A[i];

if(A[i] < min)


min = A[i];
}
printf("Largest nbr: %d\n", max);
printf("Smalles nbr: %d", min);

}
A
i max min
14 10 7 22 6 3 15 44 2 18
0 5 14 14
0 1 2 3 4 5 6 7 8 9 1 6 22 10
2 7 44 7
3 8 6
4 9 3
2

Exercise #2-
Write a program to:
a. Create an array Grades[12]
b. Fill the array with float grades between 0 and 20
c. Calculate and print the average of all grades, display the word "Passed" if the average is greater than or equals to
10, otherwise display "Failed"
d. Count and print the failed grades ( < 10 )
e. Split the array into two other arrays:
Passed[12] contains the passed grades from the array Grades
Failed[12] contains the failed grades from the array Grades
f. Display the content of the array Passed on one line
g. Display the content of the array Failed in reverse order on one line

Grades
14.5 10 0 13 6 3 15 7.5 16.5 18 1 19.5
0 1 2 3 4 5 6 7 8 9 10 11
Passed
14.5 10 13 15 16.5 18 19.5
0 1 2 3 4 5 6
Failed
0 6 3 7.5 1
0 1 2 3 4

Solution

#include <stdio.h>
void main(){
//a. Create the array
float Grades[12], Passed[12], Failed[12];
float s, av;
int i, c

//b. Fill the array with grades


for(i=0; i<12; i++){
do{
printf("Enter a grade between 0 and 20:");
scanf("%f", &Grades[i]);
}
while(Grades[i] < 0 || Grades[i] > 20 );
}
//c. Calculate the average
s = 0;
for(i=0; i<12; i++){
s = s + Grades[i];
}
av = s/12;
printf("Average: %.1f\n", av);

if(av >=10)
printf("Passed\n");
else
printf("Failed\n");

//d. Count and print the failed grades


c = 0;
for(i=0; i<12; i++){
if(Grades[i] < 10)
c = c + 1;
}

printf("Count of failed grades: %d\n", c);

//e.

for(i=0; i<12; i++){


if(Grades[i] >= 10)
Passed[ ] = Grades[i];
else
Failed[ ] = Grades[i];
}
}

You might also like