From c0f90530d70f40eadbcbd007008f92168849072b Mon Sep 17 00:00:00 2001 From: portemilio Date: Fri, 8 Jul 2016 10:10:00 -0400 Subject: [PATCH] Add files via upload --- Assignment6.txt | 28 +++++++++++++++++++++++++++ banking.c | 38 ++++++++++++++++++++++++++++++++++++ complex.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 Assignment6.txt create mode 100644 banking.c create mode 100644 complex.c diff --git a/Assignment6.txt b/Assignment6.txt new file mode 100644 index 0000000..0929079 --- /dev/null +++ b/Assignment6.txt @@ -0,0 +1,28 @@ + + 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 of this code will print employee 1's id as 203, his sex as 1, and his age as 23. + + 2. How are structures and enumerations similar and different? Give an example of when you would use each. + + Structures and enumerations are similar in that they let you define new data types. However, structures use all the memory of its members. + A structure has a separate memory location for each of its elements and they can all be used at once. + Enumerations, on the other hand, are types of data where every possible value is defined as a symbolic constant. + Enumerations do not have members. Structures do not define lists of constants. A structure can contain enumerations, but an enumeration cannot contain structures. + Enumerations help in writing clear code and simplify programming whereas structures can be more complex. + + 3. Explain the difference between passing an array directly to a function versus passing a structure containing an array? + Passing an array directly to a function passes the array to the function by reference meaning anything in the function that changes the array changes it permanently. + Passing a structure containing an array changes it by value. It makes a copy of all the values in the array so that whenever a value changes, it doesn't change the values of the original. \ No newline at end of file diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..4831423 --- /dev/null +++ b/banking.c @@ -0,0 +1,38 @@ +/* Andrew Zihenni + This code allows you to access your bank.*/ +#include +#include +typedef struct account { + int number; + char name[50]; + int balance; + +}Account; + +int deposit (Account account, int amount) { + account.balance += amount; + printf ("%s new balance is %d\n", account.name, account.balance); + return account.balance; +} +int withdraw (Account account, int amount) { + account.balance -= amount; + printf ("%s new balance is %d\n",account.name, account.balance); + return account.balance; +} +int main () { +Account account1; +Account account2; +account1.balance = 1000; +account2.balance = 5000; +strcpy (account1.name, "John"); +strcpy (account2.name, "Bob"); +account1.number = 3000; +account2.number = 2000; +printf ("%s your balance is %d\n", account1.name, account1.balance); +printf ("%s your balance is %d\n", account2.name, account2.balance); +account1.balance = deposit (account1, 3); +account2.balance = deposit (account2, 5); +account1.balance = withdraw (account1, 10); +account2.balance = withdraw (account2, 15); +return 0; +} diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..81d831a --- /dev/null +++ b/complex.c @@ -0,0 +1,51 @@ +/* Harry helped me with this. +*/ +#include + +typedef struct complexNum { + float real; + float imag; + }complexNum; +complexNum add ( complexNum Num1, complexNum Num2) { + complexNum sum; + sum.real = Num1.real + Num2.real; + sum.imag = Num1.imag + Num2.imag; + return sum; + } +complexNum multiply (complexNum Num1, complexNum Num2) { + complexNum product; + product.real = Num1.real * Num2.real - (Num1.imag * Num2.imag); + product.imag = Num1.real * Num2.imag + Num1.imag * Num2.real; + return product; + } +complexNum subtract (complexNum Num1, complexNum Num2) { + complexNum result; + result.real = Num1.real - Num2.real; + result.imag = Num1.imag - Num2.imag; + return result; + } +complexNum divide (complexNum Num1, complexNum Num2) { + complexNum quotient; + quotient.real = ((-Num1.imag * Num2.imag) - (Num1.real * Num2.real)) /(-Num2.imag*Num2.imag - Num2.real * Num2.real); + quotient.imag = (-Num1.imag * Num2.real + Num1.real * Num2.imag) / ( +-Num2.imag*Num2.imag - Num2.real * Num2.real); + return quotient; + } +int main () { +int operation; +complexNum complexNum1, complexNum2; +printf ("Input the first complex number you would like to operate on\n"); +scanf ("%fi%f", &(complexNum1.imag), &(complexNum1.real)); +printf ("Input the second complex number you would like to operate on\n"); +scanf ("%fi%f", &(complexNum2.imag), &(complexNum2.real)); +printf ("What operation would you like to perform? 1 (Addition), 2 (Substraction), 3 (Multiplication), or 4 (Division?)\n"); +scanf ("%d", &operation); +complexNum complexNum3; +switch (operation){ + case 1: complexNum3 = add(complexNum1, complexNum2);break; + case 3: complexNum3 = multiply(complexNum1, complexNum2);break; + case 4: complexNum3 = divide(complexNum1, complexNum2);break; + case 2: complexNum3 = subtract(complexNum1, complexNum2);break; +} +printf ("%fi%f", complexNum3.imag, complexNum3.real); +}