You are on page 1of 8

Experiment 1

Love Anand Adlakha 20BCE1801

Sample 1:
//@author Love Anand Adlakha 20BCE1801
//To find the Maximum and Minimum Element in an array.
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
void findmaxandmin(int *arr, int len)
{
int max = INT_MIN, min = INT_MAX;
for (int i = 0; i < len; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[i] < min)
{
min = arr[i];
}
}
printf("Maximum is : %d and minimum is %d", max, min);
}
int main()
{
printf("Enter the number of elements: ");
int len;
scanf("%d", &len);
int *arr = malloc(len * sizeof *arr);
printf("Now enter the elements:\n");
for (int i = 0; i < len; i++){
scanf("%d", &arr[i]);
}
findmaxandmin(arr, len);
}

Output :
Sample 2:
//@author Love Anand Adlakha 20BCE1801

//To count the number of odd and even elements in an array


#include <stdio.h>
#include <stdlib.h>
void findOddAndEven(int *arr, int len)
{
int oddcount = 0, evencount = 0;
for (int i = 0; i < len; i++)
{
if (arr[i] % 2 == 0)
{
evencount++;
}
else
{
oddcount++;
}
}
printf("There are %d odd numbers and %d even numbers",
oddcount, evencount);
}

int main() {
printf("Enter the number of elements: ");
int len;
scanf("%d", &len);
int *arr = malloc(len * sizeof *arr);
printf("Now enter the elements:\n");
for (int i = 0; i < len; i++){
scanf("%d", &arr[i]);
}
findOddAndEven(arr, len);
}
Output :

Exercise 1:
//@author Love Anand Adlakha 20BCE1801

#include <stdio.h>
#include <stdlib.h>
struct COLLEGE {
char name[50];
int ranking;
};
//swap two instance of the college struct
void swap(struct COLLEGE *a, struct COLLEGE *b){
struct COLLEGE temp = *a;
*a = *b;
*b = temp;
}
void tenbiggest(struct COLLEGE* arr, int len, struct COLLEGE* output){
//edge case where there are only 10, or less than 10 elements
if (len <= 10){
for (int i = 0; i < len; i++){
*output = arr[i];
output++;
}
return;
}
//Bubble sort, but stop when the 10 biggest elements
//have bubbled to the end of the array
for (int i = 0; i < 10; i++){
for (int j = 1; j < len; j++){
if (arr[j].ranking < arr[j-1].ranking){
swap(&arr[j], &arr[j-1]);
}
}
}

//add the 10 biggest elements to the output


for (int i = len-1; i > len-10-1; i--){
*output = arr[i];
output++;
}
}

int main()
{
printf("Enter the number of colleges (more than 10): ");
int len;
scanf("%d", &len);
struct COLLEGE *arr = malloc(len * sizeof *arr);
printf("Now enter the details:\n");
for (int i = 0; i < len; i++)
{
printf("Enter name of college %d: ", i+1);
scanf("%s", arr[i].name);
printf("Enter ranking of college %d: ", i+1);
scanf("%d", &arr[i].ranking);
}

struct COLLEGE biggest[10];


tenbiggest(arr, len, biggest);
free(arr);
printf("The ten best colleges are\n");
for (int i = 0; i < 10; i++){
printf("%s, ", biggest[i].name);
}
}

Output:
Exercise 2:
//@author Love Anand Adlakha 20BCE1801

#include <stdio.h>
#include <stdlib.h>
struct stock{
int* sizes; // stores stock as {small, medium, large}
} C, P;
//stores record of stock at a certain time
struct record{
struct stock C;
struct stock P;
} startofday, endofday;
int main(){
int C_start[] = {42, 36, 54};
int P_start[] = {54, 27, 30};
int C_end[] = {27, 28, 28};
int P_end[] = {31, 15, 22};
float profit[] = {0.75, 0.55, 1.20};
//hardcoded, could also be user input etc
startofday.C.sizes = C_start;
startofday.P.sizes = P_start;
endofday.C.sizes = C_end;
endofday.P.sizes = P_end;

float totalprofit = 0;
for (int i = 0; i < 3; i++){
int sold =
startofday.P.sizes[i] - endofday.P.sizes[i] +
startofday.C.sizes[i] - endofday.C.sizes[i];
totalprofit += sold * profit[i];
}
printf("The total profit is: $%0.2f", totalprofit);
}
Output:

You might also like