Skip to main content

Posts

Showing posts with the label Arrays and Pointer

C Program to Find Largest Number Using Dynamic Memory Allocation

  Find the Largest Element in a Dynamically Allocated Memory # include <stdio.h> # include <stdlib.h> int main () { int num; float *data; printf ( "Enter the total number of elements: " ); scanf ( "%d" , &num); // Allocating memory for num elements data = ( float *) calloc (num, sizeof ( float )); if (data == NULL ) { printf ( "Error!!! memory not allocated." ); exit ( 0 ); } // Storing numbers entered by the user. for ( int i = 0 ; i < num; ++i) { printf ( "Enter Number %d: " , i + 1 ); scanf ( "%f" , data + i); } // Finding the largest number for ( int i = 1 ; i < num; ++i) { if (*data < *(data + i)) *data = *(data + i); } printf ( "Largest number = %.2f" , *data); return 0 ; } Output Enter the total number of elements: 5 Enter Number 1: 3.4 Enter Number 2: 2.4 En...

C Program Swap Numbers in Cyclic Order Using Call by Reference

  Program to Swap Elements Using Call by Reference # include <stdio.h> void cyclicSwap ( int *a, int *b, int *c) ; int main () { int a, b, c; printf ( "Enter a, b and c respectively: " ); scanf ( "%d %d %d" , &a, &b, &c); printf ( "Value before swapping:\n" ); printf ( "a = %d \nb = %d \nc = %d\n" , a, b, c); cyclicSwap(&a, &b, &c); printf ( "Value after swapping:\n" ); printf ( "a = %d \nb = %d \nc = %d" , a, b, c); return 0 ; } void cyclicSwap ( int *n1, int *n2, int *n3) { int temp; // swapping in cyclic order temp = *n2; *n2 = *n1; *n1 = *n3; *n3 = temp; } Output Enter a, b and c respectively: 1 2 3 Value before swapping: a = 1 b = 2 c = 3 Value after swapping: a = 3 b = 1 c = 2

C Program to Access Array Elements Using Pointer

  Access Array Elements Using Pointers # include <stdio.h> int main () { int data[ 5 ]; printf ( "Enter elements: " ); for ( int i = 0 ; i < 5 ; ++i) scanf ( "%d" , data + i); printf ( "You entered: \n" ); for ( int i = 0 ; i < 5 ; ++i) printf ( "%d\n" , *(data + i)); return 0 ; } Output Enter elements: 1 2 3 5 4 You entered: 1 2 3 5 4

C Program to Multiply two Matrices by Passing Matrix to a Function

  Example: Multiply Matrices by Passing it to a Function # include <stdio.h> void enterData ( int firstMatrix[][ 10 ], int secondMatrix[][ 10 ], int rowFirst, int columnFirst, int rowSecond, int columnSecond) ; void multiplyMatrices ( int firstMatrix[][ 10 ], int secondMatrix[][ 10 ], int multResult[][ 10 ], int rowFirst, int columnFirst, int rowSecond, int columnSecond) ; void display ( int mult[][ 10 ], int rowFirst, int columnSecond) ; int main () { int firstMatrix[ 10 ][ 10 ], secondMatrix[ 10 ][ 10 ], mult[ 10 ][ 10 ], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k; printf ( "Enter rows and column for first matrix: " ); scanf ( "%d %d" , &rowFirst, &columnFirst); printf ( "Enter rows and column for second matrix: " ); scanf ( "%d %d" , &rowSecond, &columnSecond); // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix aga...

C Program to Find Transpose of a Matrix

  Program to Find the Transpose of a Matrix # include <stdio.h> int main () { int a[ 10 ][ 10 ], transpose[ 10 ][ 10 ], r, c, i, j; printf ( "Enter rows and columns: " ); scanf ( "%d %d" , &r, &c); // Assigning elements to the matrix printf ( "\nEnter matrix elements:\n" ); for (i = 0 ; i < r; ++i) for (j = 0 ; j < c; ++j) { printf ( "Enter element a%d%d: " , i + 1 , j + 1 ); scanf ( "%d" , &a[i][j]); } // Displaying the matrix a[][] printf ( "\nEntered matrix: \n" ); for (i = 0 ; i < r; ++i) for (j = 0 ; j < c; ++j) { printf ( "%d " , a[i][j]); if (j == c - 1 ) printf ( "\n" ); } // Finding the transpose of matrix a for (i = 0 ; i < r; ++i) for (j = 0 ; j < c; ++j) { transpose[j][i] = a[i][j]; ...

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

  Multiply Matrices by Passing it to a Function # include <stdio.h> // function to get matrix elements entered by the user void getMatrixElements ( int matrix[][ 10 ], int row, int column) { printf ( "\nEnter elements: \n" ); for ( int i = 0 ; i < row; ++i) { for ( int j = 0 ; j < column; ++j) { printf ( "Enter a%d%d: " , i + 1 , j + 1 ); scanf ( "%d" , &matrix[i][j]); } } } // function to multiply two matrices void multiplyMatrices ( int first[][ 10 ], int second[][ 10 ], int result[][ 10 ], int r1, int c1, int r2, int c2) { // Initializing elements of matrix mult to 0. for ( int i = 0 ; i < r1; ++i) { for ( int j = 0 ; j < c2; ++j) { result[i][j] = 0 ; } } // Multiplying first and second matrices and storing it in result for ( int i = 0 ; i < r1; ++i) { ...

C Program to Add Two Matrices Using Multi-dimensional Arrays

  Program to Add Two Matrices # include <stdio.h> int main () { int r, c, a[ 100 ][ 100 ], b[ 100 ][ 100 ], sum[ 100 ][ 100 ], i, j; printf ( "Enter the number of rows (between 1 and 100): " ); scanf ( "%d" , &r); printf ( "Enter the number of columns (between 1 and 100): " ); scanf ( "%d" , &c); printf ( "\nEnter elements of 1st matrix:\n" ); for (i = 0 ; i < r; ++i) for (j = 0 ; j < c; ++j) { printf ( "Enter element a%d%d: " , i + 1 , j + 1 ); scanf ( "%d" , &a[i][j]); } printf ( "Enter elements of 2nd matrix:\n" ); for (i = 0 ; i < r; ++i) for (j = 0 ; j < c; ++j) { printf ( "Enter element a%d%d: " , i + 1 , j + 1 ); scanf ( "%d" , &b[i][j]); } // adding two matrices for (i = 0 ; i < r; ++i) for (j = 0 ;...

C Program to Calculate Standard Deviation

  Program to Calculate Standard Deviation # include <math.h> # include <stdio.h> float calculateSD ( float data[]) ; int main () { int i; float data[ 10 ]; printf ( "Enter 10 elements: " ); for (i = 0 ; i < 10 ; ++i) scanf ( "%f" , &data[i]); printf ( "\nStandard Deviation = %.6f" , calculateSD(data)); return 0 ; } float calculateSD ( float data[]) { float sum = 0.0 , mean, SD = 0.0 ; int i; for (i = 0 ; i < 10 ; ++i) { sum += data[i]; } mean = sum / 10 ; for (i = 0 ; i < 10 ; ++i) SD += pow (data[i] - mean, 2 ); return sqrt (SD / 10 ); } Output Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 Standard Deviation = 2.872281

C Program to Find Largest Element in an Array

  Find the Largest Element in an array # include <stdio.h> int main () { int i, n; float arr[ 100 ]; printf ( "Enter the number of elements (1 to 100): " ); scanf ( "%d" , &n); for (i = 0 ; i < n; ++i) { printf ( "Enter number%d: " , i + 1 ); scanf ( "%f" , &arr[i]); } // storing the largest number to arr[0] for (i = 1 ; i < n; ++i) { if (arr[ 0 ] < arr[i]) arr[ 0 ] = arr[i]; } printf ( "Largest element = %.2f" , arr[ 0 ]); return 0 ; } Output Enter the number of elements (1 to 100): 5 Enter number1: 34.5 Enter number2: 2.4 Enter number3: -35.5 Enter number4: 38.7 Enter number5: 24.5 Largest element = 38.70

C Program to Calculate Average Using Arrays

  Store Numbers and Calculate Average Using Arrays # include <stdio.h> int main () { int n, i; float num[ 100 ], sum = 0.0 , avg; printf ( "Enter the numbers of elements: " ); scanf ( "%d" , &n); while (n > 100 || n < 1 ) { printf ( "Error! number should in range of (1 to 100).\n" ); printf ( "Enter the number again: " ); scanf ( "%d" , &n); } for (i = 0 ; i < n; ++i) { printf ( "%d. Enter number: " , i + 1 ); scanf ( "%f" , &num[i]); sum += num[i]; } avg = sum / n; printf ( "Average = %.2f" , avg); return 0 ; } Output Enter the numbers of elements: 6 1. Enter number: 45.3 2. Enter number: 67.5 3. Enter number: -45.6 4. Enter number: 20.34 5. Enter number: 33 6. Enter number: 45.6 Average = 27.69