Commit a293f919 by unknown

added lab 4's work

parent db3f70a5
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isDate (int input);
int largestNumber ();
int smallestNumber ();
int toBinary(int input);
int main() {
// Custom changable variables ----------------------------------
int numberToBinary = 1023;
int date = 29022100;
// End -----------------------------------------------
printf("Homework Lab 2 (10 - September - 2018)\n");
printf("Largest number in array:\t%d", largestNumber());
printf("\nSmallest number in array:\t%d", smallestNumber());
printf("\nIs valid date:\t\t\t%s", isDate(date) == 1? "True" : "False");
printf("\nBinary Homework:\t\t%d", toBinary(numberToBinary));
return (EXIT_SUCCESS);
}
int largestNumber () {
int size = 10;
int numbers[] = {15, 6, 7, 98, -8, 65, 777, -87, 213, 5};
int i;
int largest = numbers[0];
for (i = 0; i < size; i++) {
if (numbers[i] > largest) largest = numbers[i];
}
return largest;
}
int smallestNumber () {
int size = 10;
int numbers[] = {15, 6, 7, 98, -8, 65, 777, -87, 213, 5};
int i;
int smallest = numbers[0];
for (i = 0; i < size; i++) {
if (numbers[i] < smallest) smallest = numbers[i];
}
return smallest;
}
int isDate (int input) {
int day = input / 1000000;
int month = input / 10000 % 100;
int year = input % 10000;
if (year < 0) return 0;
if (month > 12) return 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) return 0;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) return 0;
}
if (month == 2) {
if (year % 4 || (year % 100 == 0 && year % 400)) {
if (day > 28) return 0;
} else {
if (day > 29) return 0;
}
}
return 1;
}
int toBinary(int input) {
if (input > 1023) return 0; // Ran out of memory error
int binLength = log(input) / log(2) + 1;
int a[binLength];
int i;
for (i = 0; i < binLength; i++) {
a[i] = input % 2;
input /= 2;
}
unsigned int result = 0;
for (i = 0; i < binLength; i++) {
result = 10 * result + a[binLength - i - 1];
}
return result;
}
#include <stdio.h>
#include <string.h>
int main() {
float orthogonalMatrix[6][6];
float initialVector[6] = {0.3, 0.4, -2, -4.4, 2.2, 3};
memcpy(orthogonalMatrix[0], initialVector, 6*sizeof(float));
float sumOfSquares;
for(int i=1; i<6; i++) {
sumOfSquares = 0;
if(initialVector[i]==0) {
orthogonalMatrix[i] [i-1]= 1;
}
for(int j=i;j<6; j++) {
sumOfSquares += initialVector[j]*initialVector[j];
}
if(sumOfSquares == 0) {
for(int j=i; j<6; j++) {
orthogonalMatrix[j][j] = 1;
}
}
initialVector[i-1] = -1*sumOfSquares/initialVector[i-1];
memcpy(orthogonalMatrix[i], initialVector, 6*sizeof(float));
initialVector[i-1] = 0;
}
for(int i=0; i<6; i++){
for(int j=0; j<6; j++){
printf("%f,", orthogonalMatrix[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define total 10
int main() {
int size = 1;
int at = 0;
int amount = 0;
int totalSize = 1;
char **list;
list = malloc(totalSize * sizeof(char*));
char * name;
name = malloc(size * sizeof(char));
printf("Please input %d names:\n", total);
char c;
for (int amount = 0; amount < total; amount++) {
size = 1;
at = 0;
while (((c = getc(stdin)) != 10)) {
name[at++] = tolower(c);
if (size == at) {
size++;
name = realloc(name, size * sizeof(char));
}
}
name[at] = '\0';
if (totalSize < strlen(name)) {
totalSize += strlen(name);
list = realloc(list, totalSize * sizeof(char*));
}
list[amount] = malloc(size * sizeof(char));
strcpy(list[amount], name);
}
printf("Comparing all names...\n");
int currentName = 0;
for (int i = 0; i < total; i++) {
if (currentName != i) {
if (strcmp(list[i], list[currentName]) == 0) {
printf("There is duplicated names you imbecile!\n");
printf("%d: %s and %d: %s\n", currentName, list[i], i, list[currentName]);
return (EXIT_SUCCESS);
}
free(list[i]);
}
}
printf("Nah fam, no duplicates\n");
free(list);
free(name);
return (EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
int * inputMatrices(int x, int y, int totalSize);
void addMatrices(int matrix1[], int matrix2[], int totalSize, int x);
void subtractMatrices(int matrix1[], int matrix2[], int totalSize, int x);
void sumDiagonalMatrices(int matrix1[], int matrix2[], int totalSize, int x);
int main() {
int x,y;
printf("Please enter matrix dimension: ");
scanf("%d", &x);
y = x;
int choice;
printf("What do you want to do?\n1: add\n2: subtract\n3: Sum of multiplication of diagonal\n");
scanf("%d", &choice);
int totalSize = x * y;
printf("Input first matrix:\n");
int *matrix1 = inputMatrices(x, y, totalSize);
printf("Input second matrix:\n");
int *matrix2 = inputMatrices(x,y, totalSize);
switch(choice) {
case 1:
addMatrices(matrix1, matrix2, totalSize, x);
break;
case 2:
subtractMatrices(matrix1, matrix2, totalSize, x);
break;
case 3:
sumDiagonalMatrices(matrix1, matrix2, totalSize, x);
break;
default:
printf("Invalid option!");
break;
}
free(matrix1);
free(matrix2);
return (EXIT_SUCCESS);
}
void addMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
for (int i = 0; i < totalSize; i++) {
printf("%d", matrix1[i] + matrix2[i]);
if (i % x == x - 1) {
printf("\n");
} else {
printf(" ");
}
}
}
void subtractMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
for (int i = 0; i < totalSize; i++) {
printf("%d", matrix1[i] - matrix2[i]);
if (i % x == x - 1) {
printf("\n");
} else {
printf(" ");
}
}
}
void sumDiagonalMatrices(int matrix1[], int matrix2[], int totalSize, int x) {
int mul1 = 1, mul2 = 1;
for (int i = 0; i < totalSize; i += (x + 1)) {
mul1 *= matrix1[i];
mul2 *= matrix2[i];
}
printf("Sum of the multiplication of elements on the diagonals: %d\n", mul1 + mul2);
}
int * inputMatrices(int x, int y, int totalSize) {
int * matrix;
matrix = malloc(x * y * sizeof(int));
for (int i = 0; i < totalSize; i++) {
printf("Please input number at %d,%d: ", i % x + 1, i / x + 1);
scanf("%d", &matrix[i]);
}
return matrix;
}
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