Docsity

Array Fundamentals in C: One and Two Dimensional Arrays, Study notes of Computer science

Toggle sidebar
bg1
Unit -5: Array
๏ƒ˜ Array is a collection of homogeneous data stored under unique name in contiguous memory
location.
๏ƒ˜ The values in an array are called as 'elements of an array'. These elements are accessed by
numbers called as 'subscripts or index numbers'.
๏ƒ˜ Array is also called 'subscripted variable'.
๏ƒ˜ Using array, you can limit the code in a program as it handles similar kind of data items also
it is efficient for solving problems like sorting, searching, indexing etc.
Array syntax:
Data_type array_name[size];
Features
a) Array size should be positive number only.
b) String array always terminates with null number ('\0).
c) Array elements are countered from 0 to n-1.
d) Useful for multiple readings of elements (numbers).
Types
a) One/Single Dimensional Array
b) Two Dimensional Array
a) One Dimensional Array: Array having only one subscript is called one dimensional array.
The values on the array are assigned one row.
Syntax
Data-type array_name [size];
Example:
Program to illustrate one dimensional array
#include<stdio.h>
#include<conio.h>
void main( )
pf3
pf4
pf5
pf8

Partial preview of the text

Download Array Fundamentals in C: One and Two Dimensional Arrays and more Study notes Computer science in PDF only on Docsity!

Unit -5: Array

๏ƒ˜ Array is a collection of homogeneous data stored under unique name in contiguous memory location. ๏ƒ˜ The values in an array are called as 'elements of an array'. These elements are accessed by numbers called as 'subscripts or index numbers'. ๏ƒ˜ Array is also called 'subscripted variable'. ๏ƒ˜ Using array, you can limit the code in a program as it handles similar kind of data items also it is efficient for solving problems like sorting, searching, indexing etc.

Array syntax:

Data_type array_name[size];

Features a) Array size should be positive number only. b) String array always terminates with null number ('\0). c) Array elements are countered from 0 to n-1. d) Useful for multiple readings of elements (numbers). Types a) One/Single Dimensional Array b) Two Dimensional Array a) One Dimensional Array: Array having only one subscript is called one dimensional array. The values on the array are assigned one row. Syntax Data-type array_name [size];

Example: Program to illustrate one dimensional array #include<stdio.h> #include<conio.h> void main( )

int a[3], i; clrscr( ); printf("Enter three numbers:"); for(i=0; i<3; i++) { scanf("%d",&a[i]); //read array } printf("Numbers are:"); for(i=0; i<3; i++) { printf("%d", a[i]); //print array } getch( ); }

Program to find the largest number among 'n' numbers. #include<stdio.h> #include<conio.h> void main( ) { int num[100], i, n, max; clrscr( ); printf("Enter the value of n:"); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter array element:"); scanf("%d",&num[i]); // read array } max = num[0]; for(i=0; i<3; i++) { if(num[i]>max) max = num[i]; } printf("The Largest Number is %d", max); getch( ); }

int a[3][3],I,j; clrscr( ); printf("Enter matrix of 33:"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { scanf("%d", &a[i][j]); //read 33 array } } printf("Matrix is:"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf("%d",a[i][j]); //print 33 array } printf("\n"); } getch( ); } Write a program to store 9 numbers with constant values in 2-dimensional array that is 3 matrix and print in matrix form. #include<stdio.h> #include<conio.h> void main ( ) { int i,j; int matrix[3][3]={100,200,300,200,150,50,350,500,600}; clrscr( ); printf("\nThe 9 elements of 3*3 matrix is:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t",matrix[i][j]); printf("\n"); } getch( ); }

Write a program to input data in 2-dimensional array for example 3*3 matrix and display in matrix form.

#include<stdio.h> #include<conio.h> void main ( ) { int i,j, a[3][3]; clrscr( ); for(i=0;i<3;i++) { for(j=0;j<3;j++) {printf("\nEnter the matrix elements:"); scanf("%d",&a[i][j]); } } printf("\nThe 3*3 matrix elements are:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t",a[i][j]); printf("\n"); } getch( ); }

Write a program to input two matrices, add two matrices and display in proper format.

#include<stdio.h> #include<conio.h> void main ( ) { int i, j, m, n, a[5][5], b[5][5], s[5][5], d[5][5]; printf("Enter the size of row and column:"); scanf("%d%d",&m,&n); printf("Enter the first matrix elements:"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("Enter the first matrix elements:"); scanf("%d",&a[i][j]); } }

  1. Write a program to show a number is less than or greater than 1 or 10.
  2. Write a program to read a number from user and determine whether the number is even or odd.

#include<stdio.h> #include<conio.h> void main() { int n=1; if(n<10) { if(n==1) {printf("the value is %d",n);} else {printf("the value is greater than 1");} } else {printf("the value is greater than 10");} getch(); }

#include<stdio.h> #include<conio.h> void main() { int num, remainder;

#include<stdio.h> #include<conio.h> void main() { float nep, eng, math, sci, soc, percent; clrscr(); printf("Enter the marks/n"); scanf("%f%f%f%f%f",&nep,&eng,&math,&sci,&soc percent=(nep+eng+math+sci+soc)/5; if(percent>=80) {printf("Distinction");} else if(percent>=60&&percent<80) {printf("First Division");} else if(percent>=45&&percent<60) {printf("Second Division");} else if(percent>=35&&percent>45) {printf(Third Division");} else {printf("Fail"); printf("Your percentage is %f,percent);} getch(); }

  1. Write a program that reads total marks of student in 5 subjects. Then calculate the percentage and determine the division