Skip to content

Instantly share code, notes, and snippets.

@swanav
Last active February 13, 2018 09:27
Show Gist options
  • Save swanav/f75ea786e19ac3dcb2b9a8a2928bce46 to your computer and use it in GitHub Desktop.
Save swanav/f75ea786e19ac3dcb2b9a8a2928bce46 to your computer and use it in GitHub Desktop.
Lab Assignment 1 for Data Structures
#include <stdio.h>
#include <stdbool.h>
/*==============================================================
Data Structures: Lab Assignment 1
Lab 1 (5 February 2018)
Lab 2 (12 February 2018)
==============================================================*/
/**************************************************************
Helper functions
***************************************************************/
void printArray(int array[], int length) {
for(int i = 0; i < length; i++)
printf("%d ", array[i]);
}
void printMatrix(int matrix[][4], int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
printf("%d\t", matrix[i][j]);
printf("\n");
}
}
void printString(char *string, int length) {
for(int i = 0; i < length; i++)
printf("%c", string[i]);
}
/*==============================================================
Question 1: WAP to swap two numbers using bitwise operator
==============================================================*/
void bitSwap(int x, int y) {
printf("\n-------------------------------Question 1-------------------------------\n");
printf("Before\tx: %d,\ty: %d\n", x, y);
x = x^y;
y = x^y;
x = x^y;
printf("After\tx: %d,\ty: %d\n", x, y);
printf("\n-------------------------------------------------------------------------\n");
}
/*===============================================================
Question 2: WAP to print the following series
a) 10,9,8,....,1
b) 2,4,6,8,...20
c) 10,13.5,17,20.5
=================================================================*/
void seriesPrint() {
printf("\n-------------------------------Question 2-------------------------------\n");
//10,9,8,....,1
for(int i = 10; i > 0; i--)
printf("%d\t", i);
printf("\n");
//2,4,6,8,...20
for(int i = 0; i < 10; i++) {
printf("%d\t", 2*(i+1));
}
printf("\n");
//10,13.5,17,20.5
for(int i = 0; i < 4; i++) {
printf("%2.1f\t", 10+3.5*i);
}
printf("\n");
printf("\n-------------------------------------------------------------------------\n");
}
/*===============================================================
Question 3: WAP to print factorial of a given number
a) Using for loop
b) Using while loop
================================================================*/
void factorial(int n) {
printf("\n-------------------------------Question 3-------------------------------\n");
// a) Using for loop
int valueFor = 1;
for(int i = 2; i <= n; i++) {
valueFor *= i;
}
printf("Factorial of %d is: %d (Using For Loop)\n", n, valueFor);
// b) Using While Loop
int valueWhile = 1;
int j = 2;
while(j<= n) {
valueWhile *= j++;
}
printf("Factorial of %d is: %d (Using While Loop)\n", n, valueWhile);
printf("\n-------------------------------------------------------------------------\n");
}
/*===============================================================
Question 4: WAP to print prime numbers from 1 to 300
================================================================*/
void primePrint(int limit) {
printf("\n-------------------------------Question 4-------------------------------\n");
for(int i = 2; i <= limit; i++) {
bool prime = true;
for(int j = 2; j < i; j++) {
if(i%j == 0) {
prime = false;
break;
}
}
if(prime)
printf("%d\t", i);
if(i%10==0) {
printf("\n");
}
}
printf("\n");
printf("\n-------------------------------------------------------------------------\n");
}
/*==================================================================
Question 5: WAP to convert temperature from celsius to fahrenheit
====================================================================*/
float celsiusToFahrenheit(float celsius) {
printf("\n-------------------------------Question 5-------------------------------\n");
printf("Temperature in celsius\t\t:\t%2.1f\n", celsius);
float fahrenheit = 9*celsius/5 + 32;
printf("Temperature in fahrenheit\t:\t%2.1f\n", fahrenheit);
printf("\n-------------------------------------------------------------------------\n");
return fahrenheit;
}
/*====================================================================
Question 6: WAP to format console output (Using \b \t \n)
======================================================================*/
void formatOutput() {
printf("\n-------------------------------Question 6-------------------------------\n");
printf("My\bName\tIs\nSwanav\n");
printf("\n-------------------------------------------------------------------------\n");
}
/*======================================================================
Question 7: WAP to solve the following problem
A library charges a fine for every book returned late. For first 5
days the fine is 50 paise, for 6-10 days the fine is one rupee and
above 10 days the fine is 5 rupees. If you return the book after 30
days your membership will be cancelled. WAP to accept no. of days
the member is late to return the book and display the fine or appr.
message.
=======================================================================*/
float libraryMembership(int days) {
printf("\n-------------------------------Question 7-------------------------------\n");
if(days > 30) {
printf("Your membership has been cancelled.");
return 0;
}
float fine = 0.0f;
if(days < 5) {
fine += 0.5*days;
} else if(days < 10) {
fine += 0.5*5 + 1*(days-5);
} else {
fine += 0.5*5 + 1*5 + 5*(days-10);
}
printf("Please pay a fine of Rs. %2.1f\n", fine);
printf("\n-------------------------------------------------------------------------\n");
return fine;
}
/*======================================================================
Question 8: WAP to display arithmetic operations using switch case
=======================================================================*/
void switchOperator(float x, float y, char operator) {
printf("\n-------------------------------Question 8-------------------------------\n");
float result;
switch(operator) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
printf("Invalid operator.");
}
printf("%2.1f %c %2.1f = %2.1f\n", x, operator, y, result);
printf("\n-------------------------------------------------------------------------\n");
}
/*=======================================================================
Question 9: WAP to print fibonnaci series upto n numbers
a) Using for loop
b) Using while loop
=========================================================================*/
void fibonnaci(int n) {
printf("\n-------------------------------Question 9-------------------------------\n");
//a) Using For loop
int arrFor[n];
arrFor[0] = 1;
arrFor[1] = 1;
for(int i = 2; i < n; i++)
arrFor[i] = arrFor[i-1] + arrFor[i-2];
printf("\nUsing For Loop: \n");
printArray(arrFor, n);
//b) Using While loop
int arrWhile[n];
arrWhile[0] = 1;
arrWhile[1] = 1;
int k = 2;
while(k < n) {
arrWhile[k] = arrWhile[k-1] + arrWhile[k-2];
k++;
}
printf("\nUsing While Loop: \n");
printArray(arrWhile, n);
printf("\n");
printf("\n-------------------------------------------------------------------------\n");
}
/*===============================================================
Question 10: WAP to find out largest element of an array
================================================================*/
void largestArray(int array[], int length) {
printf("\n-------------------------------Question 10-------------------------------\n");
printf("Array\t:\t{ ");
printArray(array, length);
printf(" }\n");
int big = 0;
for (int i = 0; i < length; i++)
if(big<array[i])
big = array[i];
printf("The largest number in the array is: %d\n", big);
printf("\n-------------------------------------------------------------------------\n");
}
/*=================================================================
Question 11: WAP to sort element of an array in ascending order
==================================================================*/
void sortArray(int array[], int length) {
printf("\n-------------------------------Question 11-------------------------------\n");
printf("Original Array\t:\t{ ");
printArray(array, length);
printf("}\n");
int big = 0;
for (int i = 0; i < length; i++) {
int small = i;
for(int j = i; j < length; j++) {
if(array[small] > array[j]) {
small = j;
}
}
int temp = array[i];
array[i] = array[small];
array[small] = temp;
}
printf("Sorted Array\t:\t{ ");
printArray(array, length);
printf("}\n");
printf("\n-------------------------------------------------------------------------\n");
}
/*==================================================================
Question 12: WAP to print the sum of each rows of a 2-D matrix
===================================================================*/
void matrixRowSum(int matrix[][4], int rows, int columns) {
printf("\n-------------------------------Question 12-------------------------------\n");
int sum[rows];
for (int i = 0; i < rows; i++) {
sum[i] = 0;
for (int j = 0; j < columns; j++) {
sum[i] += matrix[i][j];
}
}
printf("Matrix:\n");
printMatrix(matrix, rows, columns);
printf("Sum: \n");
printArray(sum, rows);
printf("\n-------------------------------------------------------------------------\n");
}
/*==============================================================
Question 13: WAP to print transpose of a matrix
===============================================================*/
void transpose(int matrix[][4], int rows, int columns) {
printf("\n-------------------------------Question 13-------------------------------\n");
printf("Original Matrix: \n");
printMatrix(matrix, rows, columns);
int transpose[rows][columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
transpose[j][i] = matrix[i][j];
//Transposed Matrix
printf("Transposed Matrix: \n");
printMatrix(transpose, rows, columns);
printf("\n-------------------------------------------------------------------------\n");
}
/*===================================================================
Question 14: WAP to convert the string from uppercase to lowercase
====================================================================*/
void stringUpperToLower(char* originalString, int stringLength) {
printf("\n-------------------------------Question 14-------------------------------\n");
printf("\nOriginal String: ");
printString(originalString, stringLength);
char convertedString[stringLength];
for(int i = 0; i < stringLength; i++) {
int x = originalString[i];
char c = x + 32;
convertedString[i] = c;
}
printf("\nConverted String: ");
printString(convertedString, stringLength);
printf("\n-------------------------------------------------------------------------\n");
}
/*=====================================================================
Question 15: WAP to copy the strings without using strcpy function
=======================================================================*/
void stringCopy(char* originalString, int stringLength) {
printf("\n-------------------------------Question 15-------------------------------\n");
printf("\nOriginal String: ");
printString(originalString, stringLength);
char copiedString[stringLength];
for(int i = 0; i < stringLength; i++)
copiedString[i] = originalString[i];
printf("\nCopied String: ");
printString(copiedString, stringLength);
printf("\n-------------------------------------------------------------------------\n");
}
/*========================================================================================
Main Function
========================================================================================*/
int main () {
printf("\n===========================Lab Assignment 1==============================\n\n");
bitSwap(10,5); //Question 1
seriesPrint(); //Question 2
factorial(5); //Question 3
primePrint(300); //Question 4
celsiusToFahrenheit(-40); //Question 5
formatOutput(); //Question 6
libraryMembership(25); //Question 7
switchOperator(60, 8, '/'); //Question 8
fibonnaci(10); //Question 9
int array[] = {10, 5, 8, 1, 7, 65, 6};
int len = 7;
largestArray(array, len); //Question 10
sortArray(array, len); //Question 11
int rows = 4;
int columns = 4;
int matrix[4][4] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{13, 14, 15, 16 }
};
matrixRowSum(matrix, rows, columns); //Question 12
transpose(matrix, rows, columns); //Question 13
char string[] = "SWANAV";
stringUpperToLower(string, 6); //Question 14
stringCopy(string, 6); //Question 15
printf("\n=========================================================================\n\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment