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
28 changes: 28 additions & 0 deletions Assignment6.txt
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions banking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Andrew Zihenni
This code allows you to access your bank.*/
#include <stdio.h>
#include <string.h>
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;
}
51 changes: 51 additions & 0 deletions complex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Harry helped me with this.
*/
#include <stdio.h>

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);
}