diff --git a/assignment6.txt b/assignment6.txt new file mode 100644 index 0000000..3d1f13f --- /dev/null +++ b/assignment6.txt @@ -0,0 +1,7 @@ +//Bettina Benitez + +1. It will be an error. This is because the struct is inside the main when functions should be written outside the main and only called in. + +2. While structs and enums both store variables, enums are for more constant variables and structs are for more flexible chaning amounts. Structs: e.g. bank balances, numbers, etc. Enums: Calendars, 7 dwarfs,etc. + +3. Passing an array directly to a function is passing the array by reference (which is local to the function) while passing a structure containing an array is passing by value (to modify the original memory location) diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..13f0135 --- /dev/null +++ b/banking.c @@ -0,0 +1,59 @@ +//Bettina Benitez +#include +#include +#include +#include + +typedef struct { + int accountNumber[2]; + char name[3][100]; + float balance[2]; +} account; + +float getDeposit (account x, int chAccount) { + printf("How much are you going to deposit into your account?: "); + float dep; //userinput for deposit + scanf("%f", &dep); + x.balance[chAccount] = x.balance[chAccount] + dep; //adds deposit + printf("\nName: %s\n Account Number: %d\n Balance: %.2f\n\n", x.name[chAccount], x.accountNumber[chAccount], x.balance[chAccount]); + return 0; +} + +float getWithdraw (account x, int chAccount) { + printf("How much are you going to withdraw from your account?: "); + float wit; //userinput for withdraw + scanf("%f", &wit); + x.balance[chAccount] = x.balance[chAccount] - wit; //adds withdraw + printf("\nName: %s\n Account Number: %d\n Balance: %.2f\n\n", x.name[chAccount], x.accountNumber[chAccount], x.balance[chAccount]); + return 0; +} + +int main () { + account x; + strcpy(x.name[1], "Tyra"); + strcpy(x.name[2], "Ally"); + srand(time(0)); + int i; + for (i = 1; i < 3; i++) { + x.accountNumber[i] = rand()%10001 + 9000; //gets random card number specific to a person + x.balance[i] = rand()%100001; // gets random balance + printf("Name: %s\n Account Number: %d\n Balance: %.2f\n\n", x.name[i], x.accountNumber[i], x.balance[i]); + } + int check = 1; + while (check != 0) { + printf("\nChoose account Tyra (1) / Ally (2): "); + int chAccount; //chosen Account + scanf("%d", &chAccount); + printf("Deposit (0) or Withdraw (1): "); + int chOption; //chosen Option + scanf("%d", &chOption); + float deposit, withdraw; + if (chOption == 0) + deposit = getDeposit(x, chAccount); + if (chOption == 1) + withdraw = getWithdraw(x, chAccount); + printf("Start a new transaction? (1) yes (0) no: "); + scanf("%d", &check); + } + return 0; +} diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..ae61c96 --- /dev/null +++ b/complex.c @@ -0,0 +1,56 @@ +//Bettina Benitez +#include + +typedef struct { + float real[2]; + float imag[2]; +} CompNum; + +void plus (float a, float b, float c, float d) { + float rtotal = a + b; + float itotal = c + d; + printf("%.0f + %.0fi\n", rtotal, itotal); +} + +void minus (float a, float b, float c, float d) { + float rtotal = a - b; + float itotal = c - d; + printf("%.0f + %.0fi\n", rtotal, itotal); +} + +void times (float a, float b, float c, float d) { + float f = a * b; + float o = a * d; + float i = c * b; + float l = (c * d) * -1; + float rtotal = f + l; + float itotal = o + i; + printf("%.0f + %0fi\n", rtotal, itotal); +} + +void divide (float a, float b, float c, float d) { + float f = a * b; + float o = a * (d * -1); + float i = c * b; + float l = c * (d * -1); + float numReal = f + (l * -1); + float numImag = o * l; + float denom = (b * b) + (d * d); + printf("%.2f/%.2f + %.2fi/%.2f", numReal, denom, numImag, denom); +} + +int main () { + char operation; + CompNum compNum; + printf("Pick Operand, Real 1, Real 2, Imaginary 1, Imaginary 2: "); + scanf("%c %f %f %f %f", &operation, &compNum.real[1], &compNum.imag[1], &compNum.real[2], &compNum.imag[2]); + if (operation == '+') + plus (compNum.real[1], compNum.real[2], compNum.imag[1], compNum.imag[2]); + if (operation == '-') + minus (compNum.real[1], compNum.real[2], compNum.imag[1], compNum.imag[2]); + if (operation == '*') + times (compNum.real[1], compNum.real[2], compNum.imag[1], compNum.imag[2]); + if (operation == '/') + divide (compNum.real[1], compNum.real[2], compNum.imag[1], compNum.imag[2]); + return 0; +} diff --git a/ststudent.c b/ststudent.c new file mode 100644 index 0000000..18fabbf --- /dev/null +++ b/ststudent.c @@ -0,0 +1,37 @@ +//Bettina Benitez +#include +#include +#include +#include + +typedef struct { + char name[5][100]; + int scores[4]; +} student; + +/*student grade(student st, int grade, int num) { + srand(time(0)); + grade = rand() %101; + st.scores[num] = grade; + return st; +}*/ + +int main () { + student x; + strcpy(x.name[0], "Bettina"); + strcpy(x.name[1], "Julia"); + strcpy(x.name[2], "Jack"); + strcpy(x.name[3], "Oscar"); + strcpy(x.name[4], "Eli"); + srand(time(0)); + int i; + float ave = 0; + for (i = 0; i < 5; i++) { + x.scores[i] = rand()%101; + printf("Name: %s \n Score: %d\n", x.name[i], x.scores[i]); + ave = ave + x.scores[i]; + } + ave = ave / 6; + printf("%.2f\n", ave); + return 0; +}