Docsity

C Programming: Understanding Arrays, Assignments of Computer Networks

Partial preview of the text

Download C Programming: Understanding Arrays and more Assignments Computer Networks in PDF only on Docsity!

Array

Array:

 (^) An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store int, char, double, float, etc.  (^) An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.  (^) int data[100]  (^) we can define an array which can store data at the contiguous memory locations.

Advantage of C Array

1) Code Optimization : Less code to the access the data.

2) Ease of traversing( travel across ) : By using the for loop, we can

retrieve the elements of an array easily.

3) Ease of sorting : To sort the elements of the array, we need a

few lines of code only.

4) Random Access : We can access any element randomly using

the array.

Declare An Array:

dataType arrayName[arraySize];

Few keynotes :

 (^) Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.  (^) If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]  (^) Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.  (^) This is because the size of a float is 4 bytes.

Change Value of Array

elementsChange Value of Array elements:

int mark[5] = {19, 10, 8, 17, 9} mark[2] = -1; // make the value of the third element to - mark[4] = 0; // make the value of the fifth element to 0 To take input from the user: scanf("%d", &mark[2]); // take input and store it in the 3rd element scanf("%d", &mark[i-1]); // take input and store it in the Ith element To print an individual element : printf("%d", mark[0]); // print the first element of the array printf("%d", mark[i-1]); // print ith element of the array

Array Input/output

#include <stdio.h> //Program to take 5 values from the user and store them in int main() { //an array, Print the elements stored in the array int values[5]; printf("Enter 5 integers: "); for(int i = 0; i < 5; i++) { // taking input and storing it in an array scanf("%d", &values[i]); } printf("Displaying integers: "); for(int i = 0; i < 5; i++) { // printing elements of an array printf("%d\n", values[i]); } return 0; }

Sorting an array:

#include<stdio.h> int main() { int n,i,j,temp,price[i]; printf("Enter the total number of prices:"); scanf("%d",&n); //for reading number form user for(i=0;i<n;i++) {printf("The price of %d is:",i+1); scanf("%d",&price[i]); }//for accending number for(i=0 ; i<n;i++) { for(j=0;j<n;j++) { if( price[j] > price[j+1] ) { temp = price[j]; price[j]= price[j+1]; price[j+1]= temp; }

//printing after assending order for(i=0;i<n;i++) { printf("%d
n",price[i]); } return 0; }

Sum of two matrices

#include<stdio.h> int main() { int i,j,a[3][3],b[3][3], result[3][3];// initializing two dimensional array printf("give the value for matrix a
n");//to read two dimensional array for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter a%d %d:",i+1,j+1);scanf("%d",&a[i][j]); } } //to read two dimensional array printf("give the value for matrix b"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter b%d%d:",i+1,j+1); scanf("%d",&b[i][j]); } } //addition two dimensional matrix for(i=0;i<3;i++) { S for(j=0;j<3;j++) { result[i][j]=a[i][j]+b[i][j]; } } //printing two dimentional array for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d
t",result[i][j]); } printf("\n"); } return 0; }