From d195a14ba5e4dffbf8b1d46564af4d5e14669134 Mon Sep 17 00:00:00 2001 From: yael kelmer Date: Thu, 7 Jul 2016 08:33:51 -0400 Subject: [PATCH 1/2] Assignment6 codes --- banking.c | 37 +++++++++++++++++++ complex.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ students.c | 46 ++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 banking.c create mode 100644 complex.c create mode 100644 students.c diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..c2af65e --- /dev/null +++ b/banking.c @@ -0,0 +1,37 @@ +/* Yael Kelmer. + This code creates accounts and uses two functions to deposit and withdraw money.*/ + +#include + +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); +} diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..a85bbf9 --- /dev/null +++ b/complex.c @@ -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 + +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; + } +} + + diff --git a/students.c b/students.c new file mode 100644 index 0000000..04c0638 --- /dev/null +++ b/students.c @@ -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 +#include + +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); + +} + From 679dafa7c460c6617174427597e595fcf64a194e Mon Sep 17 00:00:00 2001 From: yael kelmer Date: Thu, 7 Jul 2016 09:01:40 -0400 Subject: [PATCH 2/2] part2 of assignment 6 --- assignment6.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 assignment6.txt diff --git a/assignment6.txt b/assignment6.txt new file mode 100644 index 0000000..605e2ee --- /dev/null +++ b/assignment6.txt @@ -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. +