diff --git a/assignment6.txt b/assignment6.txt new file mode 100644 index 0000000..b700cca --- /dev/null +++ b/assignment6.txt @@ -0,0 +1,4 @@ +Lloyd Page +1. Errors due to missing libraries, but assuming these are included, there is still the error of having initialized within a struct. Assuming that this error is resolved, we would see an output of 203 1 23 +1. Enumerations are an advanced data type in which many variables are stored with increasing ints, starting at zero if not specified. Structures are an advanced data type in which many variables may be stored of different data types, including arrays. Example for eneumeration: Boolean data type. Example for a structure: When you want to pass by reference an array. +2. Passing an array directly to function passes it by reference, passing it in a structure passes it by value. This difference means that if you don't want to edit the array outside the function, you would want to pass it in through the function diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..8d8c782 --- /dev/null +++ b/banking.c @@ -0,0 +1,96 @@ +/*Lloyd Page*/ +/*banking accounts represtend by structures, user input for making accounts, naming them, and transactions, print all transactions, repeatable*/ +#include +#include +typedef struct +{ + int account; + float balance; + char name[100]; +} bank; +float deposit(float,float); +float withdraw(float,float); +int main() +{ + char z; + do + { + int size; + char handler[100]; + while(1) + { + printf("Enter the number of accounts\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%d",&size)) + break; + printf("Invalid input\n"); + } + bank b[size]; + for(int i=0;i=0.0)) + break; + printf("Invalid input\n"); + } + b[i].balance=withdraw(b[i].balance,draw); + } + printf("accout: %d\tname: %s\tbalance: %.2f\n",b[i].account,b[i].name,b[i].balance); + printf("Any more actions with this account?(y/n)\n"); + y=getchar(); + fgets(handler,sizeof(handler),stdin); + if(y=='n'||y=='N') + break; + }//end of while loop for an account + }//end of for loop for bank + for(int i=0;i +typedef struct +{ + int real; + int fake; +}c; +c add(c,c); +c sub(c,c); +c div(c,c); +c mul(c,c); +int main() +{ + char operation; + char handler[100]; + while(1) + { + printf("Select your operation: +,-,/,*\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%c",&operation)&&(operation=='+'||operation=='-'||operation=='/'||operation=='*')) + break; + printf("invalid option\n"); + } + c a; + c b; + c c; + while(1) + { + printf("Enter the real part of your first number\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%d",&a.real)) + break; + printf("invalid input\n"); + } + while(1) + { + printf("Enter the imaginary part of your first number\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%d",&a.fake)) + break; + printf("invalid input\n"); + } + while(1) + { + printf("Enter the real part of your second number\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%d",&b.real)) + break; + printf("invalid input\n"); + } + while(1) + { + printf("Enter the imaginary part of your second number\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%d",&b.fake)) + break; + printf("invalid input\n"); + } + if(operation=='+') + c=add(a,b); + if(operation=='-') + c=sub(a,b); + if(operation=='/') + c=div(a,b); + if(operation=='*') + c=mul(a,b); + printf("%d%+di\n",c.real,c.fake); + return 0; +} +c add(c a, c b) +{ + c c; + c.real=a.real+b.real; + c.fake=a.fake+b.fake; + return c; +} +c sub(c a, c b) +{ + c c; + c.real=a.real-b.real; + c.fake=a.fake-b.fake; + return c; +} +c div(c a, c b) +{ + c c; + c.real=(a.real*b.real+a.fake*b.fake)/(b.real*b.real+b.fake*b.fake); + return c; +} +c mul(c a, c b) +{ + c c; + c.real=a.real*b.real-(a.fake*b.fake); + c.fake=a.fake*b.real+a.real*b.fake; + return c; + +} diff --git a/struct.c b/struct.c new file mode 100644 index 0000000..13c6a63 --- /dev/null +++ b/struct.c @@ -0,0 +1,65 @@ +/*Lloyd Page*/ +/*5 student structues in an array, repeatable*/ +#include +typedef struct +{ + char name[100]; + int age; + int scores[5]; +}student; +int main() +{ + char y; + do + { + int size; + char handler[100]; + while(1) + { + printf("Enter a group size\n"); + fgets(handler,sizeof(handler),stdin); + if(sscanf(handler,"%d",&size)) + break; + printf("Enter a valid input\n"); + }/*Santizes input for size*/ + student group[size]; + for(int i=0;i