LN 3 PPT 1.pptx, arrays, rows and column

ShobanaS19 10 views 34 slides Aug 31, 2025
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

Arrays, rows and columns, matrices


Slide Content

UNIT-3 ARRAYS

ARRAY An array is a fundamental data structure that enables the storing and manipulation of potentially huge quantities of data. An array stores an ordered sequence of homogeneous values. Homogeneous means that all the values are of the same data type. An array is a collection of individual data elements that is āˆ‘ Ordered—one can count off the elements 0, 1, 2, 3, ... āˆ‘ Fixed in size āˆ‘ Homogeneous—all elements have to be of the same type, e.g., int, float, char, etc.

Declaration of a One-dimensional Array To use an array variable in a program, it must be declared. When defining an array in a program, three things need to be specified. āˆ‘ the type of data it can hold, i.e., int, char, double,float , etc. āˆ‘ the number of values it can hold, i.e., the maximum number of elements it can hold āˆ‘ a name The syntax for declaration of a one-dimensional array is data_type array_name [SIZE];

A one-dimensional array declaration is a data type followed by an identifier with a bracketed constant integral expression. The value of the expression, which must be positive, is the size of the array. It specifies the number of elements in the array. The array subscripts can range from 0 to (size –1). The lower bound of the array subscripts is 0 and the upper bound is (size –1) int a[size]; /* memory space for a[0],a[1],…, a[size –1] allocated */ lower bound = 0 upper bound = size –1 size = upper bound + 1

int ar[10]; An array ar that can hold up to 10 integers. This reserves space for 10 integers int a[100]; declares an array ā€˜a’ that can hold 100 integers.

A particular element of the array can be accessed by its ā€˜index’, a number that specifies which element is needed. The index or subscript value gives the position of the element in the array an array element can be referenced as

C does not allow declaring an array whose number of elements is unknown at compile time

Define an array size in terms of a symbolic constant, rather than a fixed integer quantity .

The number of array elements can also be given by an expression int x[N+1]; double y[M+5*N]; The C compiler must be able to evaluate the expression, which implies that all components of the expression must be available for evaluation of the expression when the program is compiled—there must be no unknowns

Storing values given by the user in an array Reading the input into an array is done as shown int a[10]; /* an array with 10 ā€œintā€ elements */ int i ; for( i =0 ; i < 10; i ++) scanf (ā€œ%dā€, &a[ i ]); The idea is that first a value must be read and copied into a[0], then another value read and copied into a[1], and so on, until all the input values have been read.

To assign values to (i.e., store values into) an array, the following statements may be used. ar[0] = 1; ar[1] = 3; ar[2] = 5; ar[3] = 7; ar[4] = 9; ar[5] = 11; ar[6] = 13; ar[7] = 15; ar[8] = 17; ar[9] = 19; OR for(i = 0; i < 10; i++) ar[i] = (i*2) + 1;

#include < stdio.h > int main() { int i ; int a[10]; for( i = 0; i < 10; i ++) { scanf ("%d", &a[ i ]); } for( i = 0; i < 10; i ++) { printf ("%d", a[ i ]); } return 0; } Reading 10 input numbers for an array and printing

#include < stdio.h > int main() { int i ; int a[10]; printf ("Enter 10 numbers:\n"); for( i = 0; i < 10; i ++) { scanf ("%d", &a[ i ]); } printf ("The numbers you entered are:\n"); for( i = 0; i < 10; i ++) { printf ("%d", a[ i ]); } return 0; }

#include < stdio.h > int main() { int i ; int a[10]; printf ("Enter 10 numbers:\n"); for( i = 0; i < 10; i ++) { scanf ("%d", &a[ i ]); } printf ("The numbers you entered are:\n"); for( i = 0; i < 10; i ++) { printf ("%d\ n", a[ i ]); } return 0; }

#include < stdio.h > int main() { int i ; int a[10]; printf ("Enter 10 numbers:\n"); for( i = 0; i < 10; i ++) scanf ("%d", &a[ i ]); printf ("The numbers you entered are:\n"); for( i = 0; i < 10; i ++) printf ("%d\ t", a[ i ]); return 0; }

#include < stdio.h > int main() { int numbers[5]; // Declare the array int total = 0; // Initialize total to zero int i ; // Declare the loop variable // Input numbers from the user printf ("Enter 5 numbers:\n"); for( i = 0; i < 5; ++ i ) { scanf ("% d",&numbers [ i ]); // Read numbers into the array } // Calculate the total for( i = 0; i < 5; ++ i ) { total = total + numbers[ i ]; } printf ("Total = %d\n", total); // Print the total } Program to input an array of 5 numbers and add using for loop 7 8 9 5 6 1 2 3 4

Operations allowed in array

#include < stdio.h > int main() { int a[20], i , m, n, r; printf ("\ nEnter the decimal integer: "); scanf ("%d", &n); m = n; // Store the original number // Convert decimal to binary for( i = 0; n > 0; i ++) { r = n % 2; // Get the remainder a[ i ] = r; // Store the binary digit n = n / 2; // Divide by 2 } printf ("\ nBinary equivalent of %d is: ", m); // Print the binary equivalent in reverse order for( i =i-1; i >= 0; i --) { printf ("%d", a[ i ]); } printf ("\n"); // New line for better formatting return 0; } TO PRINT THE BINARY EQUIVALENT OF A DECIMAL NUMBER

Read the user input and displays it in reverse order using array and loops #include < stdio.h > int main() { int a[30], n,i ; printf ("\n Enter the number n:"); scanf ("% d",&n ); for( i =0;i< n;i ++) { scanf ("% d",&a [ i ]); } printf ("\n Numbers entered in reverse order \n"); for( i =n-1; i >=0; i --) { printf ("% d",a [ i ]); } return 0; }

#include < stdio.h > int main() { int a[5], n,i ; printf ("\n Enter the number n:"); scanf ("%d",& n ); for( i =0;i< n;i ++) { scanf ("%d",& a [ i ]); } printf ("\n Numbers entered in reverse order \n"); for( i =n-1; i >=0; i --) { printf ("%d", a [ i ]); } return 0; }

char size = 1 bytes int size = 2 bytes float size = 4 bytes double size = 8 bytes

#include<stdio.h> int main() { int a[5], i ; for( i =0;i<5;++ i ) scanf ("%d",& a [ i ]); for( i =0;i<5;++ i ) printf ("%d", a [ i ]); }

Print the sum of odd and even numbers in an array #include < stdio.h > int main() { int arr [] = {6, 5, 3, 2, 7, 8, 9, 4}; int n = 8 ; // Number of elements int sum_even = 0, sum_odd = 0; // Iterate through the array for (int i = 0; i < n; i ++) { if ( arr [ i ] % 2 == 0) { sum_even = sum_even + arr [ i ]; } else { sum_odd = sum_odd + arr [ i ]; } } printf ("Sum of even numbers: %d\n", sum_even ); printf ("Sum of odd numbers: %d\n", sum_odd ); }

#include < stdio.h > int main() { int arr [] = {7, 2, 3, 4, 5, 2, 3, 2}; int n = 2; // The number to count int count = 0; int size = 8; for (int i = 0; i < size; i ++) { if ( arr [ i ] == n) { count++; } } printf ("The number %d appears %d times in the array.\n", n, count); } C program that counts the number of times a number n appears in an array.

#include < stdio.h > int main() { int arr [] = {23, 12, 56, 34, 7, 89, 45}; int size = 7 int largest = arr [0]; // Assume the first element is the largest int smallest = arr [0]; // Assume the first element is the smallest for (int i = 1; i < size; i ++) { if ( arr [ i ] > largest) { largest = arr [ i ]; // Update largest } if ( arr [ i ] < smallest) { smallest = arr [ i ]; // Update smallest } } printf ("Largest number: %d\n", largest); printf ("Smallest number: %d\n", smallest); } Program to print the largest and smallest number in an array

Dr.Sudha marks daily attendance of N students as a sequence of ā€˜P’ and ā€˜A’ for present and absent respectively help her to find the number of present and absentees for a day .

} else { printf ("Invalid attendance input. Use only 'P' or 'A'.\n"); } } printf ("Number of Present students: %d\n", precount ); printf ("Number of Absent students: %d\n", abscount ); #include<stdio.h> #include<string.h> int main() { int student, precount = 0, abscount = 0; printf ("Enter the number of students: "); scanf ("%d", &student); printf ("Enter the attendance sequence (P for present, A for absent): \n"); char attendance[student]; int i ; for ( i = 0; i < student; i ++) { scanf (" %c", &attendance[ i ]); if (attendance[ i ] == 'P' || attendance[ i ] == 'p') { precount ++; }

Calculate the ESE marks of a subject as 40% ( Internal_mark ) +60%( External_mark ) for theory subject and 60%( Internal_mark ) +40%( External_mark ) for practical subject. Internal_mark is calculated as 70%(CAT1+CAT2+AT3)/3 + 30%(Assignment1 + Assignment2 + Assignment3)/3. The External_mark isawarded for 100 .

#include < stdio.h > int main() { char subjectType ; float CAT1, CAT2, CAT3, Ass1, Ass2, Ass3; float extMark , intMark , ESE; // Input the subject type printf ("Enter the subject type (P for Practical, T for Theory): "); scanf (" %c", & subjectType ); // Input marks for CAT printf ("Enter marks for CAT1, CAT2, and CAT3: "); scanf ("%f %f %f", &CAT1, &CAT2, &CAT3); // Input marks for Assignments printf ("Enter marks for Ass1, Ass2, and Ass3: "); scanf ("%f %f %f", &Ass1, &Ass2, &Ass3); printf ("Enter external marks (out of 100): "); scanf ("%f",& extMark ); // Calculate internal marks intMark = (0.7 * (CAT1 + CAT2 + CAT3)/3) + (0.3 * (Ass1 + Ass2 + Ass3)/3); if ( subjectType == 'T' || subjectType == 't') { ESE = 0.4 * intMark + 0.6 * extMark ; } else if ( subjectType == 'P' || subjectType == 'p') { ESE = 0.6 * intMark + 0.4 * extMark ; } printf ("The ESE marks for the subject are: %.2f\n", ESE); }