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
9 changes: 9 additions & 0 deletions assignment6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Yael Kelmer.

1. Assuming that the library conio.h is present at the top of the file this is what the code will output:
It will clear the screen, then print: 203 1 23, and then wait for the user to type some character in order to exit the program, but the character will not appear on the screen.

2. Structures and enumerations are similar in that they are both abstractions to store information. However, enumerations can only store integers while structures can store strings, integers, floats, and other data types. You can use a structure to store students' names, their ages, and their avergaes. You can use an enumeration to store only the names of the students.

3. When you pass a structure containing an array, you pass by value, meaning you make a copy of the array and pass it along. When you pass an array directly to a function, you pass by reference, meaning you take that copy of the array and pass it along.

37 changes: 37 additions & 0 deletions banking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Yael Kelmer.
This code creates accounts and uses two functions to deposit and withdraw money.*/

#include <stdio.h>

typedef struct {
int accountNumber;
char accountName[100];
float accountBalance;
} bank;


bank deposit (bank b, float moneyToDeposit) {
b.accountBalance += moneyToDeposit;
return b;
}

bank withdraw (bank b, float moneyToWithdraw) {
b.accountBalance -= moneyToWithdraw;
return b;
}

int main () {

bank b1 = {3876, "checkings account", 1000};
bank b2 = {9845, "savings account", 9000};

b1 = deposit(b1, 100);
printf ("After your deposit, this is the new account balance for account %d: %f\n", b1.accountNumber, b1.accountBalance);
b1 = withdraw(b1, 400);
printf ("After your withdrawl, this is the new account balance for account %d: %f\n", b1.accountNumber, b1.accountBalance);

b2 = deposit(b2, 5000);
printf ("After your deposit, this is the new account balance for account %d: %f\n", b2.accountNumber, b2.accountBalance);
b2 = withdraw(b2, 690);
printf ("After your withdrawl, this is the new account balance for account %d: %f\n", b2.accountNumber, b2.accountBalance);
}
104 changes: 104 additions & 0 deletions complex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* Yael Kelmer.
This code uses a structure to create a complex number. The user is prompted to input two complex numbers and choose an operation. Using functions that I created the operations are executed.*/

#include <stdio.h>

typedef struct {
float realPart;
float imaginaryPart;
} complexNum;

complexNum add(complexNum c1, complexNum c2) {
float newRealPart = c1.realPart + c2.realPart;
float newImagPart = c1.imaginaryPart + c2.imaginaryPart;
complexNum result = {newRealPart, newImagPart};
return result;
}

complexNum subtract(complexNum c1, complexNum c2) {
float newRealPart = c1.realPart - c2.realPart;
float newImagPart = c1.imaginaryPart - c2.imaginaryPart;
complexNum result = {newRealPart, newImagPart};
return result;
}

complexNum multiply (complexNum c1, complexNum c2) {
float newRealPart = (c1.realPart*c2.realPart) - (c1.imaginaryPart*c2.imaginaryPart);
float newImagPart = (c1.realPart*c2.imaginaryPart) + (c1.imaginaryPart*c2.realPart);
complexNum result = {newRealPart, newImagPart};
return result;
}

complexNum divide (complexNum c1, complexNum c2) {
float newRealPart = ((c1.realPart*c2.realPart) + (c1.imaginaryPart*c2.imaginaryPart)) / ((c2.realPart*c2.realPart) + (c2.imaginaryPart*c2.imaginaryPart));
float newImagPart = ((-c1.realPart*c2.imaginaryPart) + (c1.imaginaryPart*c2.realPart)) / ((c2.realPart*c2.realPart) + (c2.imaginaryPart*c2.imaginaryPart));
complexNum result = {newRealPart, newImagPart};
return result;
}

void display (complexNum c) {
printf ("%.1f+%.1fi\n", c.realPart, c.imaginaryPart);
}

int main () {

float a;
float b;
float c;
float d;

printf ("Please print the real part of your first complex number.\n");
scanf ("%f", &a);

printf ("Please print the imaginary part of your first complex number (do not include the 'i').\n");
scanf ("%f", &b);

printf ("Please print the real part of your second complex number.\n");
scanf ("%f", &c);

printf ("Please print the imaginary part of your second complex number (do not include the 'i').\n");
scanf ("%f", &d);

complexNum c1 = {a, b};
complexNum c2 = {c, d};

printf ("This is your first complex number:");
display(c1);

printf ("This is your second complex number:");
display(c2);

printf ("Please choose and type one of the following operations to apply to the two complex numbers: + , - , * , /\n");
char operation;
scanf (" %c", &operation);

switch (operation)
{
case '+': ;
complexNum sum = add(c1, c2);
printf ("This is the sum: ");
display (sum);
printf ("\n");
break;
case '-': ;
complexNum difference = subtract(c1, c2);
printf ("This is the difference: ");
display (difference);
printf ("\n");
break;
case '*': ;
complexNum product = multiply(c1, c2);
printf ("This is the product: ");
display (product);
printf ("\n");
break;
case '/': ;
complexNum quotient = divide(c1, c2);
printf ("This is the quotient: ");
display (quotient);
printf ("\n");
break;
}
}


46 changes: 46 additions & 0 deletions students.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Yael Kelmer.
Make a structure array for 5 students that includes their name, age, and an average of 5 test scores. */

#include <stdio.h>
#include <string.h>

typedef struct {
char name[100];
int age;
int scores[5];
} student; /*this creates the structure for the student data*/

float calcAvg (student st) {
int i;
int sum = 0;
for (i = 0; i < 5; i++) {
sum += st.scores[i];
}
return (float)sum / 5;
} /*this is a function that calculates the averages for the 5 test scores for each student */

int main ()
{
/*this initializes all of the information about the students*/
student s1 = {"Anna", 15, {88, 76, 90, 57, 30} };
student s2 = {"Jake", 16, {89, 45, 67, 83, 98} };
student s3 = {"Maya", 17, {34, 99, 65, 48, 76} };
student s4 = {"Adam", 18, {65, 90, 96, 87, 43} };
student s5 = {"Drew", 19, {90, 97, 76, 89, 65} };

/*this calculates the average of the 5 grades for each student*/
float average1 = calcAvg (s1);
float average2 = calcAvg (s2);
float average3 = calcAvg (s3);
float average4 = calcAvg (s4);
float average5 = calcAvg (s5);

/*this prints out all of the information about each student*/
printf ("Student name is %s, student age is %d, and student average is %f\n", s1.name, s1.age, average1);
printf ("Student name is %s, student age is %d, and student average is %f\n", s2.name, s2.age, average2);
printf ("Student name is %s, student age is %d, and student average is %f\n", s3.name, s3.age, average3);
printf ("Student name is %s, student age is %d, and student average is %f\n", s4.name, s4.age, average4);
printf ("Student name is %s, student age is %d, and student average is %f\n", s5.name, s5.age, average5);

}