Docsity

C Programming Exercises: Arrays, Functions, Pointers, and Structures, Lab Reports of C programming

Toggle sidebar
bg1
Ex.No:2 ARRAYS AND FUNCTIONS
AIM:
To write a C Program to find min and max number in an array.
ALGORITHM:
Step 1:Start the program.
Step 2: Create two intermediate variables max and min to store the maximum and
minimum element of the array.
Step 3:Assume the first array element as maximum and minimum both, say max =
arr[0] and min = arr[0].
Step 4:Traverse the given array arr[].
Step 5: If the current element is smaller than min, then update the min as the
current element.
Step 6:If the current element is greater than the max, then update the max as the
current element.
Step 7:Repeat the above two steps 4 and 5 for the element in the array.
Step 8:Stop the program.
PROGRAM:
#include <stdio.h>
void main()
{
int arr1[100];
int i, mx, mn, n;
printf("\n\nFind maximum and minimum element in an array :\n");
printf("--------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download C Programming Exercises: Arrays, Functions, Pointers, and Structures and more Lab Reports C programming in PDF only on Docsity!

Ex.No:2 ARRAYS AND FUNCTIONS AIM: To write a C Program to find min and max number in an array. ALGORITHM: Step 1:Start the program. Step 2: Create two intermediate variables max and min to store the maximum and minimum element of the array. Step 3:Assume the first array element as maximum and minimum both, say max = arr[0] and min = arr[0]. Step 4:Traverse the given array arr[]. Step 5: If the current element is smaller than min, then update the min as the current element. Step 6:If the current element is greater than the max, then update the max as the current element. Step 7:Repeat the above two steps 4 and 5 for the element in the array. Step 8:Stop the program. PROGRAM: #include <stdio.h> void main() { int arr1[100]; int i, mx, mn, n; printf("\n\nFind maximum and minimum element in an array :\n"); printf("--------------------------------------------------\n"); printf("Input the number of elements to be stored in the array :"); scanf("%d",&n); printf("Input %d elements in the array :\n",n); for(i=0;i<n;i++) {

printf("element - %d : ",i); scanf("%d",&arr1[i]); } mx = arr1[0]; mn = arr1[0]; for(i=1; i<n; i++) { if(arr1[i]>mx) { mx = arr1[i]; } if(arr1[i]<mn) { mn = arr1[i]; } } printf("Maximum element is : %d\n", mx); printf("Minimum element is : %d\n\n", mn); }

for(j=0; j<3; j++) scanf("%d", &mat2[i][j]); } for(i=0; i<3; i++) { for(j=0; j<3; j++) matSub[i][j] = mat1[i][j] - mat2[i][j]; } printf("\nThe Subtraction Result is:\n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d ", matSub[i][j]); printf("\n"); } // getch(); return 0; }

MATRIX MULTIPLICATION

AIM:

To write a C Program for matrix multiplication using two dimensional arrays. ALGORITHM: Step 1:Start Step 2:Declare variables and initialize necessary variables Step 3:Enter the element of matrices by row wise using loops Step 4:Check the number of rows and column of first and second matrices Step 5:If number of rows of first matrix is equal to the number of columns of second matrix, go to step 6. Otherwise, print matrix multiplication is not possible and go to step 3.Multiply the matrices using nested loops. Step 5:Print the product in matrix form as console output. Step 6:Stop

PROGRAM: #include<stdio.h> #include<conio.h> int main() { int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k; printf("Enter first 33 matrix element: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) scanf("%d", &mat1[i][j]); } printf("Enter second 33 matrix element: "); for(i=0; i<3; i++)

SORT ELEMENTS IN ASCENDING ORDER.

AIM:

To write a program in C to sort elements of an array in ascending order. ALGORITHM: Step1: Start Step 2:Get the size of the array from the user. Step 3:Declare an array of given input size. Step 4:Take the input of all elements of the array. Step 5:Check If arr[i] > arr[j] and for every element check it from all the next elements to it. If the element is greater than swap that number. Step 5:Print the array elements in ascending order. PROGRAM: #include <stdio.h> void main() { int arr1[100]; int n, i, j, tmp; printf("\n\nsort elements of array in ascending order :\n "); printf("----------------------------------------------\n"); printf("Input the size of array : "); scanf("%d", &n);

printf("Input %d elements in the array :\n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); }

for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { if(arr1[j] <arr1[i]) { tmp = arr1[i]; arr1[i] = arr1[j]; arr1[j] = tmp; } } } printf("\nElements of array in sorted ascending order:\n"); for(i=0; i<n; i++) { printf("%d ", arr1[i]); } printf("\n\n"); }

int fact(int n) { if(n==0) return(1); return(n*fact(n-1)); }

SWAP TWO NUMBERS USING FUNCTION

AIM:

To write a C program to swap two numbers using call by value and call by reference. ALGORITHM: Step 1: Start the program. Step 2: Get two values of x and y. Step 3: Call the function swap(num1,num2) Step 3a: Start function. Step 3b: Assign t ← x Step 3c: Assign x ← y Step 3d: Assign y ← t Step 3e: Print x and y. Step 3f: End function. Step 4: Call the function swap(&num1,&num2) Step 4a: Start function. Step 4b: Assign t ← *x Step 4c: Assign *x ← *y Step 4d: Assign *y  t Step 4e: Print x and y. Step 4f: End function. Step 5: Stop the program. PROGRAM: #include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> void swapval(int x, int y) {

Ex.No :3 POINTERS AND STRUCTURES AIM: To write a C Program to exchange two numbers using pointers. ALGORITHM: Step 1: Start the program. Step 2: Get two values of a and b. Step 3: Call the function swap(&a,&b) Step 3a: Start function. Step 3b: Assign t ← *x Step 3c: Assign *x ← *y Step 3d: Assign *y  t Step 3e: Print x and y. Step 3f: End function. Step 4: Stop the program.

PROGRAM:

#include<stdio.h> void swap(int ,int); main( ) { int a,b; printf("Enter a and b values\n"); scanf("%d%d",&a,&b); printf ("Values before Swap\na=%d\nb=%d\n",a,b ) ; swap(&a,&b ) ; printf ("Values after Swap\na=%d\nb=%d\n",a,b ) ; } void swap( int *x, int *y )

int t ; t = *x ; *x = *y ; *y = t ; }

printf("\nEnter the name : ") ; scanf("%s", e[i].name) ; printf("\nEnter the basic pay, allowances & deductions : ") ; scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ; e[i].npay = e[i].bpay + e[i].allow - e[i].ded ; } printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ; for(i = 0 ; i < n ; i++) { printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno, e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ; } getch() ; }

Ex.No:4 FILES

AIM: To write a C program for random access file for processing the employee details. ALGORITHM: Step-1: Start the program. Step-2: Get the number of records user want to store in the system. Step-3: Using Standard Library function open the file to write the data into the file. Step-4: Store the entered information in the system. Step-5: Using do..While statement and switch case to create the options such as 1 - DISPLAY, 2.SEARCH, 3.EXIT. Step-6: Close the file using fclose() function. Step-7: Process it and display the result. Step-8: Stop the program. PROGRAM: #include <stdio.h> typedef struct { int id; char name[25]; int m1,m2,m3; }STD; STD s; void display(FILE *); int search(FILE *,int); void main() {

else printf(" Failure!! Record with id %d not found\n",id_key); break; case 3: printf(" Exit!! Press a key.. ."); getch(); break; default: printf(" Invalid Option!!! Try again !!!\n"); break; } }while(opn != 3); fclose(fp); } void display(FILE *fp) { rewind(fp); printf("ID\tName\tMarks1\tMarks2\tMarks3\n"); while(fread(&s,sizeof(s),1,fp)) printf("%d\t%s\t%d\t%d\t%d\n",s.id,s.name,s.m1,s.m2,s.m3); } int search(FILE *fp, int id_key) { rewind(fp); while(fread(&s,sizeof(s),1,fp)) if( s.id == id_key) return 1; return 0; }

Ex.No:5 ARRAY IMPLEMENTATION OF LIST ADT

AIM:

To create a list using array and perform operations such as display, insertions and deletions. ALGORITHM:

  1. Start
  2. Define list using an array of size n.
  3. First item with subscript or position = 0.
  4. Create an empty location in array after the node by moving the existing elements one position ahead.
  5. Insert the new element at appropriate position
  6. Else if choice = 2 ,Get element to be deleted.
  7. Locate the position of the element replace the element with the element in the following position.
  8. Repeat the step for successive elements until end of the array.
  9. Stop

PROGRAM:

#include<stdio.h> #include<conio.h> #define MAX 10 void create(); void insert(); void deletion(); void search(); void display(); int a,b[20],n,p,e,f,i,pos;