Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assignment6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Julia Tan
assignment6.txt

1. segmental error because struct is inside int main()
2. structures can hold many variables of different types, while enumerations can only hold int values that represent integers in ascending order. You can use enumerations as numbers and do operations on them. They are designed for variables containing a limited set of values, as it defines a set of named integer constants, starting from 0;

Structures would be useful for building a database with different types. Enums would be useful for defining the months of the year into numbers.

3. When passing an array directly, the values in the array can change so that the array will have the same value from wherever it is accessed in the program. When passing a structure containing an array, if the values in this array are changed, they are changed only within the function it has been passed to.


74 changes: 74 additions & 0 deletions banking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Julia Tan
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct {
int acctNum[3]; // Account number
char acctName[3][100]; // Account name
float balance[3]; // Account balance
} Account;

void info(Account a, int client){ // prints current account info of client
printf("Account Name: %s\n", a.acctName[client]);
printf("Account number: %d\n", a.acctNum[client]);
printf("Current balance: USD %.2f\n\n", a.balance[client]);
}

void deposit(Account a, int client, float num){ // client = which account, num = money to deposit
printf("\nUSD %.2f added to account.\n\n", num);
printf("Previous balance: USD %.2f\n\n", a.balance[client]);
a.balance[client] += num; // adds number input to total balance
info(a, client); // print account info
}

void withdraw(Account a, int client, float num){ // cleint = which account, num = money to withdraw
if (a.balance[client]-num < 0){ // If
printf("\nError: insufficient funds to do this.\n\n");
}else {
printf("\nUSD%.2f withdrawn from account.\n", num);
printf("Previous balance: %.2f\n\n", a.balance[client]);
}
a.balance[client] -= num; // subtract from balance
info(a, client); // print account info
}

int main(){
Account acc;
int num; // selects account
int wd; // withdraw or deposit
float sum; // amount of money to withdraw/deposit
int inUse = 1; // if making a transaction, equal to 1

strcpy(acc.acctName[1], "Noel Park");
strcpy(acc.acctName[2], "Sabrina Martinez");
srand(time(0));
for (int x = 1; x != 3; x ++){ // generates random acct number and acct balance
acc.acctNum[x] = (rand()%1000000);
acc.balance[x] = (rand()%1000000);
}

info(acc, 1);
info(acc, 2);

while (inUse){
printf("\nWhich account would you like to manage? (1)Noel Park (2)Sabrina Martinez\n");
scanf("%d", &num);
printf("Would you like to (1)Withdraw or (2)Deposit?\n");
scanf("%d", &wd);

if (wd == 1){
printf("How much would you like to withdraw?\n");
scanf("%f", &sum);
withdraw(acc, num, sum);
}else if (wd == 2){
printf("How much would you like to deposit?\n");
scanf("%f", &sum);
deposit(acc, num, sum);
}
printf("\nWould you like to make another transaction? (1)Yes (0)No\n");
scanf("%d", &inUse);
}
return 0;
}
72 changes: 72 additions & 0 deletions complex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Julia Tan
// complex.c

#include <stdio.h>

typedef struct{
float real[2];
float imag[2];
}Cnum;

void add(float a1, float b1, float a2, float b2){
float real = a1 + a2;
float imag = b1 + b2;
printf("Answer: %.0f+%.0fi\n", real, imag);
}

void sub(float a1, float b1, float a2, float b2){
float real = a1 - a2;
float imag = b1 - b2;
printf("Answer: %.0f+%.0fi\n", real, imag);
}

void div(float a1, float b1, float a2, float b2){
float numeratorreal;
float numeratorimag;
float denominator;
float real;
float imag;
numeratorreal = (a1*a2) + (b1*b2);
numeratorimag = (-1*a1*b2) + (b1*a2);
denominator = (a2*a2) + (b2*b2);
printf("%.2f", numeratorreal);
real = numeratorreal/denominator;
imag = numeratorimag/denominator;
printf("Answer: %.2f+%.2fi\n", real, imag);
}

void mult(float a1, float b1, float a2, float b2, int print){
float f;
float o;
float i;
float l;
float real;
float imag;
f = a1*a2; // real
o = a1*b2; // complex
i = b1*a2; // complex
l = -1*b1*b2; // real
real = f + l;
imag = o + i;
if (print)
printf("Answer: %.0f+%.0fi\n", real, imag);
}

int main(){
Cnum cnum;
char op;
printf("Enter your calculation.\nFormat: real1 imaginary1 operation real2 imaginary2\nTo write operations, type +,-,* or /.\n");
scanf("%f %f %c %f %f", &cnum.real[1], &cnum.imag[1], &op, &cnum.real[2], &cnum.imag[2]);
if (op == '+'){
add(cnum.real[1], cnum.imag[1], cnum.real[2], cnum.imag[2]);
}else if (op == '-'){
sub(cnum.real[1], cnum.imag[1], cnum.real[2], cnum.imag[2]);
}else if (op == '/'){
div(cnum.real[1], cnum.imag[1], cnum.real[2], cnum.imag[2]);
}else if (op == '*'){
mult(cnum.real[1], cnum.imag[1], cnum.real[2], cnum.imag[2], 1);
}else{
printf("Error");
}
return 0;
}
32 changes: 32 additions & 0 deletions typedef.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Julia Tan
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

typedef struct {
char name[5][100];
int scores[5];
} student;

int main(){
student s1;
strcpy(s1.name[0], "Bettina");
strcpy(s1.name[1], "Kayla");
strcpy(s1.name[2], "Alex");
strcpy(s1.name[3], "Nikki");
strcpy(s1.name[4], "Kat");
srand(time(0));
int inputScore = 0;
float average;
for (int a = 0; a != 5; a++){
inputScore = (rand()%101);
s1.scores[a] = inputScore;
printf("Name: %s\n", s1.name[a]);
printf("Score: %d\n\n", s1.scores[a]);
average += s1.scores[a];
}
printf("Class average is: %.2f\n", average/5);
return 0;
}