Created
March 29, 2022 02:43
-
-
Save inderpreet/014498f5fee1b3960c3b373425629311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
void banner(); | |
void m7_t1(); | |
void m7_t2(); | |
void m7_t3(); | |
void m8_t1(); | |
void m8_t2(); | |
void m8_t3(); | |
void m9_t1(); | |
void m9_t2(); | |
void m9_t3(); | |
void show_task(int m, int t); | |
int main() | |
{ | |
int m, t; | |
banner(); | |
m = 9; | |
t = 3; | |
show_task(m, t); | |
} | |
void banner() { | |
printf("\n*************************************************************************"); | |
printf("\nInderpreet Sing, Section 003, 320098199"); | |
printf("\n*************************************************************************"); | |
} | |
void show_task(int m, int t) { | |
switch (m) | |
{ | |
case 7: | |
switch (t) { | |
case 1: | |
m7_t1(); | |
break; | |
case 2: | |
m7_t2(); | |
break; | |
case 3: | |
m7_t3(); | |
break; | |
default: | |
printf("\nInvalid task number"); | |
} | |
break; | |
case 8: | |
switch (t) { | |
case 1: | |
m8_t1(); | |
break; | |
case 2: | |
m8_t2(); | |
break; | |
case 3: | |
m8_t3(); | |
break; | |
default: | |
printf("\nInvalid task number"); | |
} | |
break; | |
case 9: | |
switch (t) { | |
case 1: | |
m9_t1(); | |
break; | |
case 2: | |
m9_t2(); | |
break; | |
case 3: | |
m9_t3(); | |
break; | |
default: | |
printf("\nInvalid task number"); | |
} | |
break; | |
default: | |
printf("\nInvalid Module"); | |
break; | |
} | |
} | |
void m7_t1() { | |
int a[5] = { 1, 2, 3, 4, 5 }; | |
int b[5] = { 10, 20, 30, 40, 50 }; | |
int c[5]; | |
// now we meant to add a & b and put result in c | |
//c = a + b; // is this allowed? if not what is the alternative | |
// NO! This is not allowed | |
for (int j = 0; j < 5; j++) | |
c[j] = a[j] + b[j]; | |
for (int i = 1; i <= 5; i++) | |
printf("\nc[\"%i\"] = %i", i-1, c[i-1]); | |
printf("\n\n"); | |
} | |
void m7_t2() { | |
int a[5]; | |
int min, max; | |
printf("\nEnter 5 numbers: "); | |
for (int i = 0; i < 5; i++) | |
scanf_s("%d", &a[i]); | |
min = a[0]; | |
max = a[0]; | |
for (int i = 1; i < 5; i++) { | |
if (a[i]<min) | |
min = a[i]; | |
if (a[i] > max) | |
max = a[i]; | |
} | |
printf("\nMin: %d, Max: %d", min, max); | |
} | |
void m7_t3() { | |
int number_of_tests; | |
float tests[100]; | |
double total=0, average=0; | |
printf("\nEnter Number of tests: "); | |
scanf_s("%d", &number_of_tests); | |
for (int i = 0; i < number_of_tests; i++) { | |
printf("Enter mark for test %d : ", i+1); | |
scanf_s("%f", &tests[i]); | |
total += tests[i]; | |
} | |
average = total / number_of_tests; | |
printf("\nAverage is %.2lf", average); | |
} | |
void m8_t1() { | |
char first[50] = {'\0'}, last[50] = { '\0' }; | |
char fullname[61] = { '\0'}; // make null terminated | |
printf("\nEnter last name: "); | |
scanf_s("%s", last, 50); | |
printf("\nEnter first name: "); | |
scanf_s("%s", first, 50); | |
strcat_s(fullname, first); | |
strcat_s(fullname, " "); | |
strcat_s(fullname, last); | |
printf("Your name is %s\n", fullname); | |
} | |
void read_name(char* str) { | |
int i = 0; | |
do { | |
scanf_s("%c", str + i); | |
i++; | |
} while ( *(str + i - 1)!='\n'); | |
*(str + i - 1 ) = '\0'; | |
} | |
void str_cat_custom(char *dest, char* src1, char* src2) { | |
//! NOTE: In this code, substitue src1[j] for *(src1+j) to be using strings instead of pointers. | |
int i = 0; | |
int j = 0; | |
// copy src1 | |
while (*(src1 + j) != '\0') { | |
*(dest + i) = *(src1 + j); | |
i++; | |
j++; | |
} | |
// add Space | |
i++; | |
*(dest + i) = ' '; | |
// copy src2 | |
j = 0; | |
while (*(src1 + j) != '\0') { | |
*(dest + i) = *(src1 + j); | |
i++; | |
j++; | |
} | |
i++; | |
*(dest + i) = '\0'; | |
} | |
void m8_t2() { | |
char str[50] = {'\0'}; | |
char first[25] = { '\0' }, last[25] = { '\0' }; | |
printf("\nEnter full name :"); | |
read_name(str); | |
int i = 0; | |
while (str[i] != ' ') { | |
first[i] = str[i]; | |
i++; | |
} | |
int j = 0; | |
while (str[i] != '\0') { | |
last[j] = str[i]; | |
i++; | |
j++; | |
} | |
printf("\nFirst Name %s", first); | |
printf("\nLast Name %s", last); | |
} | |
void m8_t3() { | |
const int fullname_length = 15; | |
char first[50] = { '\0' }, last[50] = { '\0' }; | |
char fullname[fullname_length] = { '\0' }; // make null terminated | |
printf("\nEnter first name: "); | |
read_name(first); | |
printf("\nEnter last name: "); | |
read_name(last); | |
// str_cat_custom(fullname, first, last); | |
int i = 0; | |
while (*(first+i) != '\0' && i<fullname_length) { | |
*(fullname + i) = *(first+i); | |
i++; | |
} | |
*(fullname + i) = ' '; | |
i++; | |
int j = 0; | |
while (*(last+j)!= '\0' && i<fullname_length) { | |
*(fullname + i) = *(last+j); | |
i++; | |
j++; | |
} | |
if(i+1 <fullname_length) | |
*(fullname + i+1) = '\0'; | |
else | |
*(fullname + fullname_length-1) = '\0'; | |
printf("\nLenght of first name is %d\nLength of Last Name is %d\nTotal length + a space is %d", | |
strlen(first), | |
strlen(last), | |
strlen(first) + strlen(last) + 1 | |
); | |
if (strlen(first) + strlen(last) + 1 > 15) { | |
printf("\nFirst name too long and will be truncated to "); | |
} | |
else { | |
printf("\nThe Full name need not be shortened"); | |
} | |
printf("\n%s\n", fullname); | |
} | |
void swap(int* a, int* b) { | |
int temp; | |
temp = *a; | |
*a = *b; | |
*b = temp; | |
} | |
void m9_t1() { | |
int x_value = 24680, y_value = 13579; | |
printf("\nInitial values of x-coordinate and y-coordinate: %d , %d\n", x_value, y_value); | |
swap(&x_value, &y_value); | |
printf("\nFinal values of x-coordinate and y-coordinate: %d , %d\n", x_value, y_value); | |
} | |
void m9_t2() { | |
char str[50] = { '\0' }; | |
char first[25] = { '\0' }, last[25] = { '\0' }; | |
printf("\nEnter full name :"); | |
read_name(str); | |
int i = 0; | |
while (*(str + i) != ' ') { | |
*(first + i) = *(str + i); | |
i++; | |
} | |
int j = 0; | |
while (*(str + i) != '\0') { | |
*(last + j) = *(str + i); | |
i++; | |
j++; | |
} | |
printf("\nFirst Name %s", first); | |
printf("\nLast Name %s", last); | |
} | |
void convert(double* mag, double* rads) { | |
const double pi = 3.14159265358979323846; // the value of pi | |
double X = *mag * cos(*rads * pi /180.0); | |
double Y = *mag * sin(*rads * pi / 180.0); | |
*mag = X; | |
*rads = Y; | |
} | |
void m9_t3() { | |
double mag, angle; | |
std :: cout << std::endl <<"Enter Magnitude and Angle (in degrees) : "; | |
scanf_s("%lf %lf", &mag, &angle); | |
convert(&mag, &angle); | |
printf("\nX: %.3lf, Y: %.3lf", mag, angle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment