Skip to content

Instantly share code, notes, and snippets.

@swanav
Created February 19, 2018 13:30
Show Gist options
  • Save swanav/f9a87d2b6640780f4cd1f1ba56e152c1 to your computer and use it in GitHub Desktop.
Save swanav/f9a87d2b6640780f4cd1f1ba56e152c1 to your computer and use it in GitHub Desktop.
Lab Assignnment 3 for Data Structures
/*===============================================================================
Lab Assignment 3
Lab 3 (19 Feb 2018)
===============================================================================*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*-------------------------------------------------------------------------------
Question 1) WAP using function to multiply two numbers
-------------------------------------------------------------------------------*/
int multiply(int a, int b) {
return a*b;
}
/*-------------------------------------------------------------------------------
Question 2) WAP using function power(a,b) to calculate
the value of a raised to b
-------------------------------------------------------------------------------*/
int power(int a, int b) {
int result = 1;
for(int i = 0; i < b; i++)
result *= a;
return result;
}
/*-------------------------------------------------------------------------------
Question 3) WAP using function to swap two numbers
o pass by value
o pass by reference
-------------------------------------------------------------------------------*/
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("a: %d, b: %d\n", a, b);
return;
}
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
printf("a: %d, b: %d\n", *a, *b);
return;
}
/*-------------------------------------------------------------------------------
Question 4) WAP using function using pointers to compute the sum of all
elements stored in an array. Also display the base address and address of
each element of an array
-------------------------------------------------------------------------------*/
int arraySum(int *arr, int length) {
int sum = 0;
printf("Base address: %04x\n", arr);
for (int i = 0; i < length; i++) {
sum += arr[i];
printf("Address for element %d: %04x\n", i,arr+(i*sizeof(int)));
}
return sum;
}
/*-------------------------------------------------------------------------------
Question 5) A record contains name of cricketer, his age, and number of
test matches that he has played and the average runs that he has scored
in each test match. Create an array of structure to hold records of 20
such cricketers and then write a program to read these records and
arrange them in ascending order by average runs.
-------------------------------------------------------------------------------*/
struct PlayerData {
char[15] name;
int age;
int testMatches;
int runs;
};
/*-------------------------------------------------------------------------------
Question 6) Write a program to read, display, add, and subtract two complex
numbers using structure.
-------------------------------------------------------------------------------*/
struct Complex {
int real;
int imaginary;
};
struct Complex complexAddition(struct Complex a, struct Complex b) {
struct Complex sum;
sum.real = a.real + b.real;
sum.imaginary = a.imaginary + b.imaginary;
return sum;
}
struct Complex complexSubtraction(struct Complex a, struct Complex b) {
struct Complex sum;
sum.real = a.real - b.real;
sum.imaginary = a.imaginary - b.imaginary;
return sum;
}
void printComplexNumber(struct Complex num) {
printf("%d + %di\n", num.real, num.imaginary);
}
struct Complex inputComplex(int real, int imaginary) {
struct Complex num;
num.real = real;
num.imaginary = imaginary;
return num;
}
/*-------------------------------------------------------------------------------
Question 7) Write a program, using an array of pointers to a structure,
to read and display the data of students.
-------------------------------------------------------------------------------*/
struct Student {
char name[20];
int age;
};
struct Student getStudent(char name[], int age) {
struct Student student;
strcpy(student.name,name);
student.age = age;
return student;
}
void printStudent(struct Student student) {
printf("Student Name: %s\n", student.name);
printf("Student Age : %d\n\n", student.age);
}
/*-------------------------------------------------------------------------------
Question 8) Define a structure date containing three integers - day, month
and year. Write a program, using functions to read data, to validate the
date entered by the user and then print the date on the screen.
-------------------------------------------------------------------------------*/
struct Date {
int day;
int month;
int year;
};
struct Date readDate(int day, int month, int year) {
struct Date date;
date.day = day;
date.month = month;
date.year = year;
return date;
}
bool isValidDate(struct Date date) {
if (date.month > 12)
return false;
if (date.day > 31)
return false;
if((date.year%4==0) && (date.month==2) && (date.day > 28))
return false;
if((date.month <= 7) && (date.month%2==0) && (date.day > 30))
return false;
if((date.month > 7) && (date.month%2==1) && (date.day > 30))
return false;
return true;
}
void printDate(struct Date date) {
printf("%02d/%02d/%02d\n", date.day, date.month, date.year);
}
/*-----------------------------------------------------------------------------*/
void main() {
int a = 5;
int b = 2;
printf("%d * %d = %d\n", a, b, multiply(a,b));
printf("%d ^ %d = %d\n", a, b, power(a,b));
swapByValue(a,b);
swapByReference(&a,&b);
printf("a: %d, b: %d\n", a, b);
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
printf("Sum of all elements is: %d\n", arraySum(array, 10));
struct Complex num1, num2, sum, diff;
num1 = inputComplex(2,6);
num2 = inputComplex(4,2);
sum = complexAddition(num1, num2);
printComplexNumber(sum);
diff = complexSubtraction(num1, num2);
printComplexNumber(diff);
struct Student students[10];
char names[][10] = {
"Stuti", "Swanav", "Shubham", "Shrey", "Uday", "Viraj", "Sanidhya", "Kanishk", "Abhishek", "Simar"
};
srand(time(NULL));
for(int i = 0; i < 10; i++) {
students[i] = getStudent(names[i], rand()%60);
}
for(int i = 0; i < 10; i++) {
printStudent(students[i]);
}
struct Date date;
date = readDate(31,8,2010);
if(isValidDate(date))
printDate(date);
else
printf("Date is Invalid\n");
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment