diff --git a/Assignment6.txt b/Assignment6.txt new file mode 100644 index 0000000..87d5367 --- /dev/null +++ b/Assignment6.txt @@ -0,0 +1,13 @@ +Clarke Littlejohn +This mist likely has a lot of typos as i was extremely tried when doing this. + +1) if i recall correctly it shouldnt print anything as the unassigned int have a special % thing +but if that is not it then it print out the 8 1 7 as those are predefined. but i would think it would crash. + +2)structes and enum are similiar at all really. enums from what i know seem goot for calendars and thats it +where structures are seem very similiar to objects in java or c++ which makes them much more useful. enums to me seems pointless it like an +array but instead f x[0] you can call it by a name. + +3)passing an array directly passes it by reference so it makes so that it it can changed wuth un that function and the chnages will stay +whereas the a struct that has an array and that is being passed is passed by value. Si a copied is made and any changes that are made are +done in the original array. diff --git a/bank.c b/bank.c new file mode 100644 index 0000000..3e0cd7f --- /dev/null +++ b/bank.c @@ -0,0 +1,192 @@ +//Clarke Littlejohn +//Banking program +#include +#include +#include +#include + +typedef struct{ + int ID; + float balance; + char fName[100]; + char lName[100]; + char password[100]; +}bankAcc; +void accountSetup(char); +float deposit(char[],bankAcc); +float withdraw(char[],bankAcc); +void updateInfo(bankAcc); +void menu(); + +int main(){ + accountSetup('i'); + return 0; +} +void accountSetup(char dw){ + + srand(time(0)); + int temp=0; + char input[3], input1[100]; + static bankAcc user; + + //i couldn't think of any other way to make sure the values were being passed correctly since the user is declared here + if(dw=='D'||dw=='W'||dw=='Q') + goto DWQ; + + //gets user input to see if the person wants to set up an account + printf("Bank\n"); + do{ + printf("Do you want to set up an account? [Y/N] "); + if(temp>0) + printf("\nEnter Y for yes N for no. "); + fgets(input,sizeof(input),stdin); + input[strlen(input)-1]='\0'; + if(strcmp(input,"Y")==0){ + break; + } + else { + if(strcmp(input,"N")==0){ + printf("Ending program...\n"); + return; + } + } + temp++; + }while(1); + + //these 3 blocks of code get the name and password of the account. + //could have done this in a 2D array but i was too lazy to look up how to print 2d arrays. + printf("\nEnter your first name: "); + fgets(input1,sizeof(input1),stdin); + input1[strlen(input1)-1]='\0'; + strcpy(user.fName,input1); + + printf("\nEnter your last name: "); + fgets(input1,sizeof(input1),stdin); + input1[strlen(input1)-1]='\0'; + strcpy(user.lName,input1); + + printf("\nEnter your Password: "); + fgets(input1,sizeof(input1),stdin); + input1[strlen(input1)-1]='\0'; + strcpy(user.password,input1); + + //confirmation on password. + int tracker=0; + do{ + if(tracker==0) + printf("\nConfirm your Password: "); + else + printf("Password does not match. Confirm your Password: "); + fgets(input1,sizeof(input1),stdin); + input1[strlen(input1)-1]='\0'; + if(strcmp(user.password,input1)==0) + break; + tracker++; + }while(1); + + user.ID=rand(); + printf("Your ID number is: %d",user.ID); + menu(); + + //skips over the code aboce as that only needs to be done once +DWQ: if(dw=='D'){ + printf("\nEnter your Password: "); + fgets(input1,sizeof(input1),stdin); + input1[strlen(input1)-1]='\0'; + user.balance=deposit(input1,user); + updateInfo(user); + menu(); + } else if(dw=='W'){ + printf("\nEnter your Password: "); + fgets(input1,sizeof(input1),stdin); + input1[strlen(input1)-1]='\0'; + user.balance=withdraw(input1,user); + updateInfo(user); + menu(); + } else if(dw=='Q'){ + updateInfo(user); + printf("\nEnding Program...\n"); + } + +} + + +void menu(){ + char input[3]; + int i=1; + //get the type of transaction was going to add more things to the menu + do + { + printf("\nWhat transaction would you like to do?\n(D)Deposit, (W)Withdraw, or (Q)Quit: "); + + fgets(input,sizeof(input),stdin); + input[strlen(input)-1]='\0'; + if(input[0]=='D' || input[0]=='W' || input[0]=='Q'){ + accountSetup(input[0]); + i=0; + + } + }while(i); +} + +float deposit(char password[], bankAcc user){ + + //makes sure the password is correct before entering it, there should be some type of lock or cooldown period but can think of a way to + //code that. + char userError [100]; + float money=0; + if(strcmp(user.password,password)!=0){ + while(strcmp(user.password,userError)!=0){ + printf("\nInvaild password. Reenter your password: "); + fgets(userError,sizeof(userError),stdin); + userError[strlen(userError)-1]='\0'; + } + } + + //deposits money + printf("\nEnter the amount of money you want to add to your account: "); + scanf("%f",&money); + fgets(userError,sizeof(userError),stdin); + userError[strlen(userError)-1]='\0'; + return user.balance+money; +} + +float withdraw(char password[], bankAcc user){ + + //makes sure the password is correct before entering it, there should be some type of lock or cooldown period but can think of a way to + //code that. + char userError [100]; + float money=0; + if(strcmp(user.password,password)!=0){ + while(strcmp(user.password,userError)!=0){ + printf("\nInvaild password. Reenter your password: "); + fgets(userError,sizeof(userError),stdin); + userError[strlen(userError)-1]='\0'; + } + } + + //withdraws money + printf("\nEnter the amount of money you want to take out of your account: "); + scanf("%f",&money); + fgets(userError,sizeof(userError),stdin); + userError[strlen(userError)-1]='\0'; + return user.balance-money; +} + +void updateInfo(bankAcc user){ +printf("\nName: %s %s\nID Number: %d\nBalance: $%.2f",user.fName,user.lName,user.ID,user.balance); +} + + + + + + + + + + + + + + diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..2139f8b --- /dev/null +++ b/banking.c @@ -0,0 +1,137 @@ +//Clarke Littlejohn +//Banking program i used code from another banking program i wrote so is probably is not as clean as it should be. +#include +#include +#include +#include + +typedef struct{ + int ID; + float balance; + char fName[100]; + char lName[100]; +}bankAcc; +float deposit(bankAcc); +float withdraw(bankAcc); +void updateInfo(bankAcc); +char menu(); + +int main(){ + + srand(time(0)); + bankAcc user[2]; + user[0].ID=rand(); + user[0].balance=10000; + strcpy(user[0].fName,"john"); + strcpy(user[0].lName,"doe"); + updateInfo(user[0]); + + +char x; + do{ + x=menu(); + if(x=='D'){ + user[0].balance=deposit(user[0]); + updateInfo(user[0]); + x=menu(); + } else if(x=='W'){ + user[0].balance=withdraw(user[0]); + updateInfo(user[0]); + x=menu(); + } else if(x=='Q'){ + updateInfo(user[0]); + printf("\nEnding Program...\n"); + } + else + x=menu(); + }while(x!='Q'); + + + + + + user[1].ID=rand(); + user[1].balance=23234; + strcpy(user[1].fName,"jane"); + strcpy(user[1].lName,"doe"); + updateInfo(user[1]); + + do{ + x=menu(); + if(x=='D'){ + user[1].balance=deposit(user[1]); + updateInfo(user[1]); + x=menu(); + } else if(x=='W'){ + user[1].balance=withdraw(user[1]); + updateInfo(user[1]); + x=menu(); + } else if(x=='Q'){ + updateInfo(user[1]); + printf("\nEnding Program...\n"); + } + else + x=menu(); + }while(x!='Q'); + return 0; +} +char menu(){ + char input[3]; + int i=1; + //get the type of transaction was going to add more things to the menu + do + { + printf("\nWhat transaction would you like to do?\n(D)Deposit, (W)Withdraw, or (Q)Quit: "); + + fgets(input,sizeof(input),stdin); + input[strlen(input)-1]='\0'; + if(input[0]=='D' || input[0]=='W' || input[0]=='Q'){ + i=0; + + } + }while(i); +return input[0]; +} + +float deposit(bankAcc user){ + + float money; + char userError[100]; + + //deposits money + printf("\nEnter the amount of money you want to add to your account: "); + scanf("%f",&money); + fgets(userError,sizeof(userError),stdin); + userError[strlen(userError)-1]='\0'; + return user.balance+money; +} +float withdraw(bankAcc user){ + + float money; + char userError[100]; + + //withdraws money + printf("\nEnter the amount of money you want to take out of your account: "); + scanf("%f",&money); + fgets(userError,sizeof(userError),stdin); + userError[strlen(userError)-1]='\0'; + return user.balance-money; +} + +void updateInfo(bankAcc user){ +printf("\nName: %s %s\nID Number: %d\nBalance: $%.2f",user.fName,user.lName,user.ID,user.balance); +} + + + + + + + + + + + + + + diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..3d39642 --- /dev/null +++ b/complex.c @@ -0,0 +1,126 @@ +//Clarke Littlejohn +//Complex numbers +//could get the division to work i should split it up into multiple parts. + +#include +#include + + +typedef struct{ + float real; + float img; +}complx; +complx addition(complx,complx); +complx subtraction(complx,complx); +complx division(complx,complx); +complx multiplication(complx,complx); +void printcomplx(complx); + +int main(){ + + char input[3]; + complx nums[2]; + float stor; + + printf("Enter + for addition, - for subtraction, * for multiplication, and / for division: "); + fgets(input,sizeof(input),stdin); + input[1]='\0'; + + printf("Enter in the first real number: "); + scanf("%f",&stor); + nums[0].real=stor; + printf("Enter in the first imaginary number: "); + scanf("%f",&stor); + nums[0].img=stor; + printf("Enter in the second real number: "); + scanf("%f",&stor); + nums[1].real=stor; + printf("Enter in the second imaginary number: "); + scanf("%f",&stor); + nums[1].img=stor; + + switch(input[0]){ + case '+': + printcomplx(addition(nums[0],nums[1])); + break; + case '-': + printcomplx(subtraction(nums[0],nums[1])); + break; + case '*': + printcomplx(multiplication(nums[0],nums[1])); + break; + case '/': + printcomplx(division(nums[0],nums[1])); + break; + default: + printf("Restart the program"); + break; + } + return 0; +} + +complx addition(complx fZero,complx fOne){ + + complx fTwo; + + fTwo.real=(fZero.real+fOne.real); + fTwo.img=(fZero.img+fOne.img); + return fTwo; + +} + +complx subtraction(complx fZero,complx fOne) + +{ + complx fTwo; + + fTwo.real=(fZero.real-fOne.real); + fTwo.img=(fZero.img-fOne.img); + return fTwo; +} + +complx multiplication(complx fZero,complx fOne){ + + complx fTwo; + + fTwo.real=(fZero.real*fOne.real)+(fZero.img*fOne.img); + fTwo.img=(fZero.real*fOne.img)+(fZero.img*fOne.real); + return fTwo; +} + +complx division(complx fZero,complx fOne){ + + complx fTwo; + + fTwo.real=((fZero.real*fOne.real)+(fZero.img* (-1*fOne.img)))/((fOne.real*fOne.real)+(fOne.img*(-1*fOne.img))); + fTwo.img=(fZero.real*fOne.img)+(fZero.img*fOne.real)/((fOne.real*fOne.real)+(fOne.img*(-1*fOne.img))); + return fTwo; + +} + +void printcomplx(complx out){ + + printf("The answer is %.2f+%.2fi\n",out.real,out.img); +} + + + + + + + + + + + + + + + + + + + + + + diff --git a/struct.c b/struct.c new file mode 100644 index 0000000..7b17b28 --- /dev/null +++ b/struct.c @@ -0,0 +1,89 @@ +//Clarke Littlejohn +//inclass assignment +#include +#include +#include +#include + +typedef struct{ + int age; + int scores[5]; + int avg; + char name[100]; +}student[5]; + +int average(int[],int); + +int main(){ + srand(time(0)); + char userIn[100]; + int age; + student test; + int i; + int j; + //gets namesof students + printf("Enter the name of the five students: "); + for(i=0;i<5;i++){ + fgets(userIn,sizeof(userIn),stdin); + userIn[strlen(userIn)-1]='\0'; + strcpy(test[i].name,userIn); + + } + //gets age of students + printf("Enter the age of the five students:\n"); + for(i=0;i<5;i++){ + printf("%s's Age: ",test[i].name); + scanf("%d",&age); + test[i].age=age; + } + //generates grade of students + for(i=0;i<5;i++){ + + for(j=0;j<5;j++) + { + int k=(rand()%100+1); + int l=rand()%2; + if(l) + k+=5; + test[i].scores[j]=k; + } + } + //gets average of students + for(i=0;i<5;i++){ + test[i].avg=average(test[i].scores,(sizeof(test[i].scores)/4)); + } + //output + for(i=0;i<5;i++){ + printf("\nName: %s\tAge: %d\tTest Average: %d\n",test[i].name,test[i].age,test[i].avg); + } + return 0; +} + +int average(int score[],int size){ + int i=0,avg=0; + + for(;i