Commit bcd6ca80 by unknown

Lab 5 tasks

parent a293f919
...@@ -14,7 +14,7 @@ int main() { ...@@ -14,7 +14,7 @@ int main() {
char **list; char **list;
list = malloc(totalSize * sizeof(char*)); list = malloc(totalSize * sizeof(char*));
char * name; char *name;
name = malloc(size * sizeof(char)); name = malloc(size * sizeof(char));
printf("Please input %d names:\n", total); printf("Please input %d names:\n", total);
......
...@@ -11,19 +11,20 @@ int main() { ...@@ -11,19 +11,20 @@ int main() {
printf("Please enter matrix dimension: "); printf("Please enter matrix dimension: ");
scanf("%d", &x); scanf("%d", &x);
y = x; y = x;
int choice; int choice;
printf("What do you want to do?\n1: add\n2: subtract\n3: Sum of multiplication of diagonal\n"); printf("What do you want to do?\n1: add\n2: subtract\n3: Sum of multiplication of diagonal\n");
scanf("%d", &choice); scanf("%d", &choice);
int totalSize = x * y; int totalSize = x * y;
printf("Input first matrix:\n"); printf("Input first matrix:\n");
int *matrix1 = inputMatrices(x, y, totalSize); int *matrix1 = inputMatrices(x, y, totalSize);
printf("Input second matrix:\n"); printf("Input second matrix:\n");
int *matrix2 = inputMatrices(x,y, totalSize); int *matrix2 = inputMatrices(x,y, totalSize);
switch(choice) { switch(choice) {
case 1: case 1:
addMatrices(matrix1, matrix2, totalSize, x); addMatrices(matrix1, matrix2, totalSize, x);
...@@ -38,8 +39,8 @@ int main() { ...@@ -38,8 +39,8 @@ int main() {
printf("Invalid option!"); printf("Invalid option!");
break; break;
} }
free(matrix1); free(matrix1);
free(matrix2); free(matrix2);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
...@@ -53,7 +54,7 @@ void addMatrices(int matrix1[], int matrix2[], int totalSize, int x) { ...@@ -53,7 +54,7 @@ void addMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
} else { } else {
printf(" "); printf(" ");
} }
} }
} }
void subtractMatrices(int matrix1[], int matrix2[], int totalSize, int x) { void subtractMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
...@@ -64,11 +65,11 @@ void subtractMatrices(int matrix1[], int matrix2[], int totalSize, int x) { ...@@ -64,11 +65,11 @@ void subtractMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
} else { } else {
printf(" "); printf(" ");
} }
} }
} }
void sumDiagonalMatrices(int matrix1[], int matrix2[], int totalSize, int x) { void sumDiagonalMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
int mul1 = 1, mul2 = 1; int mul1 = 1, mul2 = 1, mul1x;
for (int i = 0; i < totalSize; i += (x + 1)) { for (int i = 0; i < totalSize; i += (x + 1)) {
mul1 *= matrix1[i]; mul1 *= matrix1[i];
mul2 *= matrix2[i]; mul2 *= matrix2[i];
...@@ -85,5 +86,3 @@ int * inputMatrices(int x, int y, int totalSize) { ...@@ -85,5 +86,3 @@ int * inputMatrices(int x, int y, int totalSize) {
} }
return matrix; return matrix;
} }
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char names[5][20] = {
"Bob",
"Michael",
"max",
"Mjchael",
"andrei"
};
int main() {
int length = 5;
int isSorted = 0;
while(!isSorted) {
isSorted = 1;
length--;
for (int i = 0; i < length; i++) {
char name1[20], name2[20];
strlcpy(name1, names[i], sizeof(names[i]));
strlcpy(name2, names[i + 1], sizeof(names[i + 1]));
// Too lazy to compare 1st 5 char, so i compared all
if (strcmp(strlwr(name1), strlwr(name2)) > 0) {
isSorted = 0;
char buff[20];
strlcpy(buff, names[i], sizeof(names[i]));
strlcpy(names[i], names[i + 1], sizeof(names[i + 1]));
strlcpy(names[i + 1], buff, sizeof(buff));
}
}
}
for (int i = 0; i < 5; i++) {
printf("%s\n", names[i]);
}
return (EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
void task1Function(void) {
int N, i, j;
scanf("%d", &N);
int array[N];
for (i = 0; i < N; i++) {
scanf("%d", &array[i]);
}
for (i = 0; i < N; i++) {
for (j = 0; j < N - 1; j++) {
if (array[j] < array[j + 1]) {
int swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int square(int input) {
int out = input * input;
return out;
}
float getAngle(int vec1[], int vec2[]) {
float mag1 = (float)(sqrt(square(vec1[0]) + square(vec1[1]) + square(vec1[2])));
float mag2 = (float)(sqrt(square(vec2[0]) + square(vec2[1]) + square(vec2[2])));
int dotProd = vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2];
float cos = (float)(dotProd / (mag1 * mag2));
return (float)acos(cos) * 180 / M_PI;
}
void printVector(int vec[], char buffer[]) {
snprintf(buffer, 20 * sizeof(char), "( %d, %d, %d )", vec[0], vec[1], vec[2]);
}
int main1() {
int vec[4][3] = {
{1, 4, 6},
{2, -3, 1},
{3, 5, 0},
{3, 1, -3}
};
int start = 1;
for (int i = 0; i < 4; i++) {
for (int j = start; j < 4; j++) {
char buffer1[20];
char buffer2[20];
printVector(vec[i], buffer1);
printVector(vec[j], buffer2);
printf("%s and %s:\t", buffer1, buffer2);
printf("%f degree\n",getAngle(vec[i], vec[j]));
}
start++;
}
return (EXIT_SUCCESS);
}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char emails[9][30] = {
"omeguy@rusty.edu",
"milfHunter@gmail.com",
"smarmy@yahoo.com",
"h8sgmail.com@yahoo.com",
"smokinRocks@gmail.com",
"whodoesthis?@hotmail.com",
"smallPerson@hotmail.com",
"WhereIntheW0rld@WIW.com",
"mom_has_a_computer@aol.com"
};
int main2() {
int isGmail = 0;
for (int i = 0; i < 8; i++) {
int size = sizeof(emails[i]) / sizeof(char);
char email[size];
strcpy(email, emails[i]);
char buffer[size];
int isAt = 0;
for (int j = 0; j < size; j++) {
if (email[j] == '@') {
isAt = 1;
if(email[j + 1] == 'g') isGmail++;
}
if (!isAt) buffer[j] = email[j];
}
buffer[size] = '\0';
printf("%s\n", buffer);
}
printf("Number of gmails: %d\n", isGmail);
return (EXIT_SUCCESS);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment