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.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Assignment 6 Sheqi Zhang

1. What will be output of following C code? Explain your answer.

``` C
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();
}
```
Answer:
It just print out the id, sex and age for emp1.

2. How are structures and enumerations similar and different? Give an example of when you would use each.
Answer:
Structure is a group of variables with different datatypes, while enumeration is a data type where every possible value is defined as a symbolic constant. Structure is just the combination of some varibales, while the format of enumeration will automatically change the value of some variables in it. The similarity is that they all handle some variables.I can see structure when I have to define an individual with several different data types, like defining the bank account. I can see enum when expressing the months.

3. Explain the difference between passing an array directly to a function versus passing a structure containing an array?
Answer:
If a array is passing to a function contained in a function, it is passed by value. However, if a array is directly passing to a function, it is passing by reference.
40 changes: 40 additions & 0 deletions banking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>

typedef struct bankAccount{
char name;
float balance;
int accountNumber;
}Account;

float money(float x){
float balance;
printf("Please type in the amount of money you want to change:\n");
scanf("%f",&x);
balance=balance+x;
return balance;
}

int main(){
Account ac1;

strcpy(ac1.name,"Marry");
ac1.balance=5000;
ac1.accountNumber=123;

float x;

money(x);
printf("The change in your account is \"%f\", your balance is \"%f\"\n",x,ac1.balance);

Account ac2;

strcpy(ac2.name,"Maria");
ac2.balance=10000;
ac2.accountNumber=456;

money(x);
printf("The change in your account is \"%f\", your balance is \"%f\" \n",x,ac2.balance);

return 0;
}
38 changes: 38 additions & 0 deletions complexNumber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <complex.h>

typedef struct complexNumber(float x, float y){
double z = x+y*I;
return complex;
}complex;

int main(){
float x1,x2,y1,y2;
printf("Please type in two real number:\n");
scanf("%f,%f",&x1,&x2);
printf("Please type in two imaginary part:\n");
scanf("%f,%f",&y1,&y2);

complex z1,z2;

char c;
printf("Please choose a kind of calculation:\nA.addition\nB.subtraction\nC.multiplication\nD.division\n");
scanf("%c",&c);

switch(c){
case"A":
float sum = z1+z2;
printf("Sum=\"%f\"+\"%f\"i/n",creal(sum),cimag(sum));
case "B":
float difference=z1-z2;
printf("Difference=\"%f\"+\"%f\"i/n",creal(difference),cimag(difference));
case "C":
float product=z1*z2;
printf("Product=\"%f\"+\"%f\"i/n",creal(product),cimag(product));
case "D":
float quotient=z1/z2;
printf("Quotient=\"%f\"+\"%f\"i/n",creal(quotient),cimag(quotient));
}

return 0;
}
50 changes: 50 additions & 0 deletions studentsScore.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

typedef struct {
char name[100];
int age;
int scores[5];
}student;


int main(){
srand(time(0));
student s1,s2,s3,s4,s5;

strcpy(s1.name,"Anna");
strcpy(s2.name,"Jane");
strcpy(s3.name,"Kelly");
strcpy(s4.name,"Tom");
strcpy(s5.name,"Hellen");


int studentsScore[5]={s1.scores,s2.scores,s3.scores,s4.scores,s5.scores};
int j=0;
while(j<=5){
int i=0;
int scores[5];
while(i<=5){
int r= rand();
scores[i]=rand()%100;
i++;
}
studentsScore[j]=scores[5];
j++;
continue;}

printf("s1 name:\"%s\"/n",s1.name);
printf("s1 scores:\"%d \"/n",s1.scores);
printf("s2 name:\"%s\"/n",s2.name);
printf("s2 scores:\"%d \"/n",s2.scores);
printf("s3 name:\"%s\"/n",s3.name);
printf("s3 scores:\"%d \"/n",s3.scores);
printf("s4 name:\"%s\"/n",s4.name);
printf("s4 scores:\"%d \"/n",s4.scores);
printf("s5 name:\"%s\"/n",s5.name);
printf("s5 scores:\"%d \"/n",s5.scores);
return 0;
}