diff --git a/assignment6.txt b/assignment6.txt new file mode 100644 index 0000000..5c1a4a5 --- /dev/null +++ b/assignment6.txt @@ -0,0 +1,8 @@ +Ava N. +July 6, 2016 + +Assignment 6: + +1. struct cannot be put inside of main(). +2. Structures are collections of related variables under a single name. Enumerates define a set of named integer canstants starting from 0. +3. Passing an array directly to a function as an argument. Passing a structure containing an array causes the array to be copied to the function's parameter. diff --git a/bank b/bank new file mode 100755 index 0000000..ac520a5 Binary files /dev/null and b/bank differ diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..a83ce58 --- /dev/null +++ b/banking.c @@ -0,0 +1,52 @@ +/*Ava N.*/ +//This program uses structures to represent a bank account. The accounts have an account number, name, and balance. I created functions to deposit or withdraw money from the balance. Write a driver that creates 2 accounts, adds money to each, then withdraws money from each. Print details of the account between each transaction. + +#include +#include +#include + +typedef struct{ + char name[2]; + int balance; + float age; +} account; + +int main(){ + account a1, a2; + strcpy(a1.name, "Business"); + strcpy(a2.name, "Home"); + + int i; + + a1.balance = 0; + a2.balance = 0; + +//Deposit part: + printf("How much money do you want to deposit into the %s\t account? Amount of money in account: %d\n", a1.name, a1.balance); + scanf("%d", &i); + a1.balance = a1.balance + i; + + printf("How much money do you want to deposit into the %s\t account? Amount of money in account: %d\n", a2.name, a2.balance); + scanf("%d", &i); + a2.balance = a2.balance + i; + + +//Withdrawal part: + printf("How much money do you want to withdraw into the %s\t account? Amount of money in account: %d\n", a1.name, a1.balance); + scanf("%d", &i); + a1.balance = a1.balance - i; + + printf("How much money do you want to withdraw from the %s\t account? Amount of money in account: %d\n", a2.name, a2.balance); + scanf("%d", &i); + a2.balance = a2.balance - i; + + + printf("\n"); + + printf("Your Receipt: \n"); + printf("You have %d dollars in your %s\t account \n", a1.balance, a1.name); + printf("You have %d dollars in your %s\t account \n", a2.balance, a2.name); + + return 0; + +} diff --git a/cat b/cat new file mode 100644 index 0000000..e69de29 diff --git a/complex b/complex new file mode 100755 index 0000000..ea0a2f8 Binary files /dev/null and b/complex differ diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..9dce75f --- /dev/null +++ b/complex.c @@ -0,0 +1,88 @@ +/*Ava N.*/ +//This program implements complex numbers as a structure and allows a user to perform calculations with them. The structure contains real and imaginary portions of a complex number. The functions add, subtract, multiply, divide and display complex numbers. I have allowed the user to select what functions they want to use (much like my calculator.c programs from last week) and enter the complex numbers they want to perform calculations with. + +#include +#include + +typedef struct { + int real; //real + int imag; //imaginary +} complx; + +int main(){ + complx f; //first number + complx s; //second number + complx a; //answer + complx o; //operation + + printf("Choose one of the following: +, -, *, /, or %%: \n"); + scanf("%d\n", &o.real); + + printf("Pick a real number: \n"); + scanf("%d\n", &f.real); + + printf("Pick another real number: \n"); + scanf("%d\n", &s.real); + + printf("Choose one of the following: +, -, *, /, or %%: \n"); + scanf("%d\n", &o.imag); + + printf("Pick an imaginary number: \n"); + scanf("%d\n", &f.imag); + + printf("Pick another imaginary number: \n"); + scanf("%d\n", &s.imag); + + switch(o.real){ + case '+': + a.real=f.real+s.real; + printf("The addition is: %d\n", a.real); + break; + case '-': + a.real=f.real-s.real; + printf("The subtraction is: %d\n", a.real); + break; + case '/': + a.real=f.real/s.real; + printf("The division is: %d\n", a.real); + break; + case '*': + a.real=f.real*s.real; + printf("The multiplication is: %d\n", a.real); + break; + case '%': + a.real=(int)f.real%(int)s.real; + printf("The percentage is: %d\n", a.real); + break; + default: + printf("Your response was invalid. Please try again."); + } + switch(o.imag){ + case '+': + a.imag=f.imag+s.imag; + printf("The addition is: %d\n", a.imag); + break; + case '-': + a.imag=f.imag-s.imag; + printf("The subtraction is: %d\n", a.imag); + break; + case '/': + a.imag=f.imag/s.imag; + printf("The division is:%d \n", a.imag); + break; + case '*': + a.imag=f.imag*s.imag; + printf("The multiplication is: %d\n", a.imag); + break; + case '%': + a.imag=(int)f.imag%(int)s.imag; + printf("The percentage is: %d\n", a.imag); + break; + default: + printf("Your response was invalid. Please try again."); + } + printf("%d \n", a.imag); + printf("%d \n", a.real); + + return 0; +} diff --git a/less b/less new file mode 100644 index 0000000..e69de29 diff --git a/structure b/structure new file mode 100755 index 0000000..6039135 Binary files /dev/null and b/structure differ diff --git a/student_structure.c b/student_structure.c new file mode 100644 index 0000000..97b0c5b --- /dev/null +++ b/student_structure.c @@ -0,0 +1,107 @@ +/*Ava N.*/ +//Student structure with 5 names, their grades (scores on tests), their ages, and an average of their grades. + +#include +#include + +typedef struct{ + char name[100]; + int age; + int scores[5]; +} student; + +student grade(student st, int grade, int num) { + st.scores[num]=grade; + return st; +} +float average(student st, int no_of_grades){ + float sum=0; + for(int num=0 ; num < no_of_grades ; num++){ + sum += st.scores[num]; + } + + return sum/no_of_grades; + } + +int main(){ + student s1, s2, s3, s4, s5; + strcpy(s1.name, "Sally"); + strcpy(s2.name, "Curtis"); + strcpy(s3.name, "Marianne"); + strcpy(s4.name, "John"); + strcpy(s5.name, "Peter"); + + + s1.age = 15; + s2.age = 16; + s3.age = 17; + s4.age = 100; + s5.age = -1; + + printf("Name: %s\tAge: %d\n", s1.name, s1.age); + printf("Name: %s\tAge: %d\n", s2.name, s2.age); + printf("Name: %s\tAge: %d\n", s3.name, s3.age); + printf("Name: %s\tAge: %d\n", s4.name, s4.age); + printf("Name: %s\tAge: %d\n", s5.name, s5.age); + + s1=grade(s1, 100, 0); + s1=grade(s1, 96, 1); + s1=grade(s1, 99, 2); + s1=grade(s1, 100, 3); + s1=grade(s1, 100, 4); + s2=grade(s2, 94, 0); + s2=grade(s2, 60, 1); + s2=grade(s2, 40, 2); + s2=grade(s2, 94, 3); + s2=grade(s1, 100, 4); + s3=grade(s3, 96, 0); + s3=grade(s3, 100, 1); + s3=grade(s3, 94, 2); + s3=grade(s3, 96, 3); + s3=grade(s3, 100, 4); + s4=grade(s4, 90, 0); + s4=grade(s4, 68, 1); + s4=grade(s4, 81, 2); + s4=grade(s4, 75, 3); + s4=grade(s4, 100, 4); + s5=grade(s5, 90, 0); + s5=grade(s5, 68, 1); + s5=grade(s5, 30, 2); + s5=grade(s5, 75, 3); + s5=grade(s5, 100, 4); + /* + printf("Grade: %d\n", s1.scores[0]); + printf("Grade: %d\n", s1.scores[1]); + printf("Grade: %d\n", s1.scores[2]); + printf("Grade: %d\n", s1.scores[3]); + printf("Grade: %d\n", s2.scores[0]); + printf("Grade: %d\n", s2.scores[1]); + printf("Grade: %d\n", s2.scores[2]); + printf("Grade: %d\n", s2.scores[3]); + printf("Grade: %d\n", s3.scores[0]); + printf("Grade: %d\n", s3.scores[1]); + printf("Grade: %d\n", s3.scores[2]); + printf("Grade: %d\n", s3.scores[3]); + printf("Grade: %d\n", s4.scores[0]); + printf("Grade: %d\n", s4.scores[1]); + printf("Grade: %d\n", s4.scores[2]); + printf("Grade: %d\n", s4.scores[3]); + printf("Grade: %d\n", s5.scores[0]); + printf("Grade: %d\n", s5.scores[1]); + printf("Grade: %d\n", s5.scores[2]); + printf("Grade: %d\n", s5.scores[3]); +*/ + + printf("\n"); + + + printf("Average grade of Sally: %f \n",average(s1, 4)); //average(structure,no. of grades) + printf("Average of Curtis: %f \n",average(s2, 4)); + printf("Average of Marianne: %f \n",average(s3, 4)); + printf("Average of John: %f \n",average(s4, 4)); + printf("Average of Peter: %f \n",average(s5, 4)); + + + return 0; + +}