From 63aaffa5f32f5924c4747f8a0d293e23910083ba Mon Sep 17 00:00:00 2001 From: elik723 Date: Thu, 7 Jul 2016 09:45:39 -0400 Subject: [PATCH] Here is Assignment 6 for Eli Kalish --- Assignment6.txt | 34 +++++++++++++++++++++++++++++++ banking.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ complex.c | 46 ++++++++++++++++++++++++++++++++++++++++++ studAve.c | 25 +++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 Assignment6.txt create mode 100644 banking.c create mode 100644 complex.c create mode 100644 studAve.c diff --git a/Assignment6.txt b/Assignment6.txt new file mode 100644 index 0000000..a05b265 --- /dev/null +++ b/Assignment6.txt @@ -0,0 +1,34 @@ +1. What will be output of following C code? Explain your answer. +int main() { + struct employee + { + unsigned int id = 8; + unsigned int sex = 1; + unsigned int age = 7; + }; + struct employee emp1={203,1,23}; + clrscr(); + printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age); + getch(); +} + The output would either be an error. Structures must be declared outside the main statement. + Since it isn't here, the rest of the code calling upon the structure would just come up + with errors. +2. How are structures and enumerations similar and different? Give an example of when you would +use each. + Structures and enumerations are both similar in that they can hold many different values + in one package(This is probably the wrong word for that). They're different in that a + structure can hold multiple different variables and/or different vairable types, whereas + enums are more like symbols for auto-set values. An example for enums is for the monthes + of the year and assigning each of them their corresponding value (January is 1, February + is 2, etc). Structures to me seem to have a much wider use and are used to store lots + of related data for a specific person/thing. +3. Explain the difference between passing an array directly to a function versus passing +a structure containing an array? + Normally an array is pass-by reference, meaning when is used in a function, a new reference + to the existing array is created rather than a new identical array. An array within a + structure, however, can be passed by value, or more accurately called by value. When + you have an array within a structure, and then pass that structure to a function, + the array is copied as it would be if it were pass by value. + + diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..d549a97 --- /dev/null +++ b/banking.c @@ -0,0 +1,53 @@ +#include + +typedef struct BankAccount +{ + char name[100]; + int account; + float balance; +} bank; + +float withdraw(float a) +{ + printf("How much money do you want to withdraw?\n"); + float w; + scanf("%f", &w); + printf("Previous balance: %f\nCurrent balance: %f\n", a, a - w); + return a - w; +} + +float deposit(float a) +{ + printf("How much money do you want to deposit?\n"); + float d; + scanf("%f", &d); + printf("Previous balance: %f\nCurrent balance: %f\n", a, a + d); + return a + d; +} + +int main() +{ + printf("This is a bank. Time to set up your account.\n"); + bank cust1 = {"Johnny Money", 214365, 60000.00}; + printf("Your name is %s, your account number is %d, and you currently have $%f\n", cust1.name, cust1.account, cust1.balance); + printf("I would make it so you can have your own money and name but nobody got time for that.\n"); + int cont = 0; + do { + printf("Welcome to the bank operations screen, %s. You currently have $%f\n", cust1.name, cust1.balance); + printf("Withdraw (1), Deposit (2), or exit (0)?\n"); + int user; + scanf("%d", &user); + switch (user) { + case 1: + cust1.balance = withdraw(cust1.balance); + break; + case 2: + cust1.balance = deposit(cust1.balance); + break; + case 0: + printf("Thanks for coming to the bank, please come again.\n"); + cont++; + } + } while (cont == 0); +return 0; +} diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..38501ff --- /dev/null +++ b/complex.c @@ -0,0 +1,46 @@ +#include + +typedef struct ComplexNumbers { + int real; + int i; +} comp; + +int main() +{ + printf("This program performs calculations on two complex numbers.\n"); + printf("First please enter the real part of the complex number, enter, and then the imaginary part(but don't include i when you type it)\n"); + printf("Please enter the first complex number\n"); + comp c1; + scanf("%d", &c1.real); + scanf("%d", &c1.i); + printf("You entered %d + %di\n", c1.real, c1.i); + printf("Please enter the second complex number\n"); + comp c2; + scanf("%d", &c2.real); + scanf("%d", &c2.i); + printf("You entered %d + %di\n", c2.real, c2.i); + printf("What computation would you like to perform?\n?"); + printf("Addition (1), Subtraction (2), Multiplication (3), or Division (4)?\n"); + int user; + scanf("%d", &user); + switch (user) + { + case 1: + printf("You chose addition\n"); + printf("(%d + %di) + (%d + %di) = %d + %di\n", c1.real, c1.i, c2.real, c2.i, c1.real + c2.real, c1.i + c2.i); + break; + case 2: + printf("You chose subtraction\n"); + printf("(%d + %di) - (%d + %di) = %d + %di\n", c1.real, c1.i, c2.real, c2.i, c1.real - c2.real, c1.i - c2.i); + break; + case 3: + printf("You chose multiplication\n"); + printf("(%d + %di) * (%d + %di) = %d + %di\n", c1.real, c1.i, c2.real, c2.i, c1.real * c2.real - c1.i * c2.i, c1.real * c2.i + c1.i * c2.real); + break; + case 4: + printf("You chose division\n"); + printf("(%d + %di) / (%d + %di) = %f + %fi\n", c1.real, c1.i, c2.real, c2.i, (c1.real * c2.real + c1.i * c2.i)/ (float) (c2.real * c2.real + c2.i * c2.i), (c1.i * c2.real - c1.real * c2.i) / (float) (c2.real * c2.real + c2.i * c2.i)); //I get this is stupidly long but at this point I'm just proud it works + break; + } + return 0; +} diff --git a/studAve.c b/studAve.c new file mode 100644 index 0000000..c8a1d79 --- /dev/null +++ b/studAve.c @@ -0,0 +1,25 @@ +#include +#include + +typedef struct { + char name[100]; + int age; + int scores[5]; +} student; + +int main() +{ + student s1 = {"Boogie", 16, {75, 80, 85, 90, 95}}; + student s2 = {"Richard Nixon", 103, {61, 57, 100, 89, 74}}; + student s3 = {"Fork on the Left", 4, {40, 80, 100, 95, 90}}; + student s4 = {"Caroline", 17, {93, 95, 96, 97, 100}}; + student s5 = {"Sylvanas Windrunner", 45, {84, 76, 98, 77, 65}}; + student store[5] = {s1, s2, s3, s4, s5}; + for (int i = 0; i < 5; i++) + { + float ave = (store[i].scores[0]+store[i].scores[1]+store[i].scores[2]+store[i].scores [3]+store[i].scores[4])/ 5.0; + printf("%s: %f\n", store[i].name, ave); + } + return 0; +} +