0% found this document useful (0 votes)
11 views14 pages

Lab 7. Psat

Programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Lab 7. Psat

Programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1-D array problem

Sample program to read and display an array using functions

Code : #include <stdio.h>

// Function to read elements into the array


void readArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
}

// Function to display elements of the array


void displayArray(int arr[], int size) {
printf("Array elements are: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int size;

// Ask the user for the array size


printf("Enter the size of the array: ");
scanf("%d", &size);

int arr[size]; // Declare the array

// Call functions to read and display the array


readArray(arr, size);
displayArray(arr, size);

return 0;
}
Output :

2. // reverse array

#include<stdio.h>

void read(int arr[] ,int n)

for(int i=0;i<n;i++)

scanf("%d",&arr[i])

;}

void disp(int arr[],int n)

{for (int i=0;i<n;i++)

{printf("%d\n",arr[i]);

}}

void show(int arr[],int n)

for(int i=n-1;i>=0;i--)

{
printf("%d\n",arr[i] );

} printf("\n"); }

int main(){int n ;

printf("enter the number of elements");

scanf("%d",&n);

int arr[n];

read(arr,n);

disp(arr,n);

show(arr,n);

return 0;}

//difference between maximum and minimum elements

#include<stdio.h>

void read(int arr[],int n)

for(int i=0;i<n;i++)

scanf("%d",&arr[i]);

void bubble(int arr[],int n)

for( int i=0;i<n;i++)

for(int j=0;j<n-i-1;j++)
{

if(arr[j]>arr[j+1])

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;}}}}

int diff(int arr[],int n)

int end=n-1;

int i=0;

int diff=arr[end]-arr[i];

return diff ; }

void disp(int arr[],int n)

for(int i=0;i<n;i++)

{ printf("%d\n",arr[i]);}

int main(){int n;

printf("enter the size of array");

scanf("%d",&n);

int arr[n];

read(arr,n);

disp(arr,n);

bubble(arr,n);

printf(" the difference is %d\n",diff(arr,n));

return 0;}
//second element

#include<stdio.h>

void read(int arr[] ,int n)

for(int i=0;i<n;i++)

scanf("%d",&arr[i])

;}

void sort(int arr[],int n)

for (int i =0;i<n;i++)

for(int j=0;j<n-i-1;j++)

{if (arr[j]>arr[j+1])

int temp=arr[j+1];

arr[j+1]=arr[j];

arr[j]=temp;}

}}}

void disp(int arr[],int n)

{int end=n-1;

printf("%d",arr[end-1]);

void show(int arr[],int n)

{
for(int i=0;i<n;i++)

printf("%d",arr[i] );

} printf("\n"); }

int main(){int n ;

printf("enter the number of elements");

scanf("%d",&n);

int arr[n];

read(arr,n);

show(arr,n);

sort(arr,n);

disp(arr,n);

return 0;}

//duplicates

#include<stdio.h>

void read(int arr[] ,int n)

for(int i=0;i<n;i++)

scanf("%d",&arr[i])

;}

void sort(int arr[],int n)

for (int i =0;i<n;i++)

{
for(int j=0;j<n-i-1;j++)

{if (arr[j]>arr[j+1])

int temp=arr[j+1];

arr[j+1]=arr[j];

arr[j]=temp;}

}}}

void disp(int arr[],int n)

{for (int i=0;i<n;i++)

{if(arr[i]==arr[i+1] )

printf(" the duplicates are %d",arr[i]);

}}}

void show(int arr[],int n)

for(int i=0;i<n;i++)

printf("%d\n",arr[i] );

} printf("\n"); }

int main(){int n ;

printf("enter the number of elements");

scanf("%d",&n);

int arr[n];

read(arr,n);

show(arr,n);

sort(arr,n);

disp(arr,n);
return 0;}

//missing number

#include<stdio.h>

void read(int arr[] ,int n)

for(int i=0;i<n-1;i++)

scanf("%d",&arr[i])

;}

void sort(int arr[],int n)

for (int i =0;i<n-1;i++)

{
for(int j=0;j<n-i-1;j++)

{if (arr[j]>arr[j+1])

int temp=arr[j+1];

arr[j+1]=arr[j];

arr[j]=temp;}

}}}

void disp(int arr[],int n)

{for (int i=0;i<n-2;i++)

{if(arr[i+1]!=arr[i]+1 )

printf(" the missing elements is %d",arr[i]+1);

}}}
void show(int arr[],int n)

for(int i=0;i<n-1;i++)

printf("%d\n",arr[i] );

} printf("\n"); }

int main(){int n ;

printf("enter the number of elements");

scanf("%d",&n);

int arr[n];

read(arr,n);

show(arr,n);

sort(arr,n);

disp(arr,n);
return 0;}

//negative numbers

#include<stdio.h>

void read(int arr[] ,int n)

for(int i=0;i<n;i++)

scanf("%d",&arr[i])

;}

void sort(int arr[],int n)

for (int i =0;i<n;i++)

for(int j=0;j<n-i-1;j++)

{if (arr[j]>arr[j+1])

int temp=arr[j+1];

arr[j+1]=arr[j];

arr[j]=temp;}

}}}

void disp(int arr[],int n)

{for (int i=0;i<n;i++)

{printf("%d\n",arr[i]);
}}

void show(int arr[],int n)

for(int i=0;i<n;i++)

printf("%d\n",arr[i] );

} printf("\n"); }

int main(){int n ;

printf("enter the number of elements");

scanf("%d",&n);

int arr[n];

read(arr,n);

show(arr,n);

sort(arr,n);

disp(arr,n);

return 0;}

Call by Value Swap Function:

Code :

#include <stdio.h>

// Function to swap values using call by value


void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;

printf("Inside swapByValue: a = %d, b = %d\n", a, b);


}

int main() {
int x = 10, y = 20;

printf("Before swapByValue: x = %d, y = %d\n", x, y);


swapByValue(x, y);
printf("After swapByValue: x = %d, y = %d\n", x, y);

return 0;
}

Observation : here in this code we are reading the value of x and y from the
user after this we have created a swap functiion . In that swap function the
value of x and y is copied to a and b respectively .

Now we are printing this value in the function. After that in the main loop we
are printintg the value of x and y .first we printed it before the function call it
will give the value before swapping then we called the function and it printed
the swap value of the x and y. now again we printed x and y and it gives the
original value of x without swapping due to the scope of variable.

Output :

Call by Reference Swap


Function:

In call by reference, the addresses of the variables are passed to the


function, allowing it to modify the original values directly.

#include <stdio.h>

// Function to swap values using call by reference


void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;

printf("Inside swapByReference: a = %d, b = %d\n", *a, *b);


}

int main() {
int x = 10, y = 20;

printf("Before swapByReference: x = %d, y = %d\n", x, y);


swapByReference(&x, &y);
printf("After swapByReference: x = %d, y = %d\n", x, y);

return 0;
}

You might also like