From 182f0777f3bea09fe7581fbd9cc688de96d7114e Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 6 Jul 2016 22:20:41 -0400 Subject: [PATCH 1/2] codes for assignment 6 --- banking.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ structex.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 banking.c create mode 100644 structex.c diff --git a/banking.c b/banking.c new file mode 100644 index 0000000..d2ed13f --- /dev/null +++ b/banking.c @@ -0,0 +1,64 @@ +/* banking code + @author Aly Milich + */ + +#include + +typedef struct bank{ + + int accNum; + char name[100]; + float balance; + +} bank; + + +float withdraw(float a){ + + float newBal; + newBal = 1000-a; + printf("Your new balance is: %f", newBal); +} + +float deposit(float a, bank b){ + + float nuevoBal; + b.balance = b.balance + a; + + printf("Your new balance is: %f", b.balance); +} + + + +int main(){ + char name[100]; + printf("What is your name?\n"); + fgets("%s", &name); + + printf("Do you want to deposit(press 1) or withdraw(press 2)\n"); + bank bank1 = {1, name, 1000}; + bank bank2 = {2, name, 800}; + printf("#: %d, Name: %s, Balance: %f\n",bank1.accNum,bank1.name,bank1.balance); + int input; + scanf("%d", &input); + + if(input == 1){ + float depo; + + printf("How much are you depositing?"); + scanf("%f", &depo); + + deposit(depo, bank1); + } + + else if(input ==2){ + float withd; + + printf("How much are you withdrawing?"); + scanf("%f", &withd); + + withdraw(withd); + + } + +} diff --git a/structex.c b/structex.c new file mode 100644 index 0000000..37563bd --- /dev/null +++ b/structex.c @@ -0,0 +1,56 @@ +#include +#include + +typedef struct { + char name[100]; + int age; + int scores[5]; + int average; +} student; + +int main(){ + student s1= {"Emma", 17, {45, 35, 60, 70, 90}}; + student s2 = {"Katie", 19, {45, 35, 60, 70, 90}}; + student s3 = {"Victoria", 16, {65, 75, 85, 92, 82}}; + student s4 = {"Vicky", 12, {55, 25, 60, 70, 90}}; + student s5 = {"Deluca", 90, {70, 80, 90, 100, 90}}; + + student s[] = {s1, s2, s3, s4, s5}; + + int i, sum=0, j; + + for(i=0; i<5; i++){ + sum= 0; + for(j=0; j<5; j++){ + sum = sum + s[i].scores[j]; + + } + s[i].average = sum/5; + + } + + //print students + + printf("Name: %s\n", s[0].name); + printf("Age: %d\n", s[0]. age); + printf("Average: %d\n", s[0].average); + printf("\n"); + printf("Name: %s\n", s[1].name); + printf("Age: %d\n", s[1].age); + printf("Average: %d\n", s[1].average); + printf("\n"); + printf("Name: %s\n", s[2].name); + printf("Age: %d\n", s[2]. age); + printf("Average: %d\n", s[2].average); + printf("\n"); + printf("Name: %s\n", s[3].name); + printf("Age: %d\n", s[3].age); + printf("Average: %d\n", s[3].average); + printf("\n"); + printf("Name: %s\n", s[4].name); + printf("Age: %d\n", s[4].name); + printf("Average: %d\n", s[4].average); + + return 0; + +} From 5919d3bdd6a9ea0db0dbaf48fb36c1eec54f5cf1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 6 Jul 2016 22:21:06 -0400 Subject: [PATCH 2/2] questions for assignment 6 --- assignment6.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 assignment6.txt diff --git a/assignment6.txt b/assignment6.txt new file mode 100644 index 0000000..1c7d5e5 --- /dev/null +++ b/assignment6.txt @@ -0,0 +1,7 @@ +Aly Milich + +1. Structures need to be declared before the main so you will get an error if you try to declare struct inside the main. + +2. Structures are used to store different variable types under one common structure. You could use these to store different data about a student which could include their gender (char), name (string), and age (int). Enumerations assign variable names to stored integers which could be useful in storing the months for example because then the name of the month and its corresponding number would match up. + +3. Passing an array directly versus passing a structure containing an array is the difference between passing by value and calling by value. In the former, the array is only changed inside the function whereas in the latter, the values of the array are changed until the program terminates.