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
4 changes: 4 additions & 0 deletions assignment6.txt
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions banking.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
#include<string.h>
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<size;i++)
{
b[i].account=i;
printf("Enter your name\n");
fgets(b[i].name, sizeof(b[i].name),stdin);
b[i].name[strlen(b[i].name)-1]='\0';
while(1)
{
char y;
printf("Deposit money?(y/n)\n");
y=getchar();
fgets(handler,sizeof(handler),stdin);
if(y=='y'||y=='Y')
{
float dep;
while(1)
{
printf("Enter the amount\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%f",&dep))
break;
printf("Invalid input\n");
}
b[i].balance=deposit(b[i].balance,dep);
printf("accout: %d\tname: %s\tbalance: %.2f\n",b[i].account,b[i].name,b[i].balance);
}
printf("Withdraw money?(y/n)\n");
y=getchar();
fgets(handler,sizeof(handler),stdin);
if(y=='y'||y=='Y')
{
float draw;
while(1)
{
printf("Enter the amount\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%f",&draw)&&(withdraw(b[i].balance,draw)>=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<size;i++)
printf("accout: %d\tname: %s\tbalance: %.2f\n",b[i].account,b[i].name,b[i].balance);
printf("Run again?(y/n)\n");
z=getchar();
fgets(handler,sizeof(handler),stdin);
}while(z=='y'||z=='Y');
return 0;
}
float deposit(float a, float b)
{
a+=b;
return a;
}
float withdraw(float a, float b)
{
a=a-b;
return a;
}
98 changes: 98 additions & 0 deletions complexcal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*Lloyd Page*/
/*Calculator for complex numbers*/
#include<stdio.h>
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;

}
65 changes: 65 additions & 0 deletions struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*Lloyd Page*/
/*5 student structues in an array, repeatable*/
#include<stdio.h>
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<size;i++)
{
printf("Enter student%d's name\n",i+1);
fgets(group[i].name,sizeof(group[i].name),stdin);
while(1)
{
printf("Enter student%d's age\n",i+1);
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&group[i].age))
break;
printf("Enter a valid input\n");
}
for(int j=0;j<sizeof(group[i].scores)/4;j++)
{
while(1)
{
printf("Enter a student%d's grade for test%d\n",i+1,j+1);
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&group[i].scores[j]))
break;
printf("Enter a valid number\n");
}
}
}
for(int i=0;i<size;i++)
{
float avg=0;
for(int j=0;j<sizeof(group[i].scores)/4;j++)
{
avg+=group[i].scores[j];
}
avg=avg/((float)(sizeof(group[i].scores))/4);
printf("Name: %s Age: %d Average score: %g\n",group[i].name,group[i].age,avg);
}
printf("Run again?(y/n)");
y=getchar();
fgets(handler,sizeof(handler),stdin);
}while(y=='y'||y=='Y');
return 0;
}