In: Computer Science
3) Create a nested structure (one structure that contains another) and print out all the fields in both structures. The main structure should have fields for: movie name and the year it was released. The extended structure should include the original structure as well as fields for: Lead actor, genre and runtime.
In C
4) Create a structure for an employee which contains a field for: first name, last name, id and salary. Then use printf and scanf to fill the structure. (Hint, you’ll have to use strcpy)
5) Make a counter using a for loop that counts from 0 to 200 in increments of 5.
6) Make a counter using a for loop that counts down from 200 to 0.
7) Create an array with 10 numbers, then print out each element plus the next element in the array. Note for the last element there will be no next element so there is no need to add to that element.
(a.)
#include<stdio.h>
#include<string.h>
/* FIRST STRUCTURE CONTAINS MOVIE NAME AND YEAR */
struct First
{
char mname[50];
int year;
};
/* STRUCTURE SECOND CONTAINS STRUCTURE FIRST VARIABLE, LEAD ACTOR, GENRE AND RUNTIME */
struct Second
{
struct First f1;
char lactor[50];
char genre[50];
int runtime;
};
int main(){
struct Second s1 = {"Avengers", 2019, "Tony Stark", "Fantasy", 120};
printf("%s %d %s %s %d", s1.f1.mname, s1.f1.year, s1.lactor, s1.genre, s1.runtime);
return 0;
}
(b.)
#include<stdio.h>
#include<string.h>
struct Employee{
char fname[50];
char lname[50];
int id;
int salary;
};
int main(){
char temp[50];
struct Employee e1;
/* TAKING EMPLOYEE DETAILS AS INPUT */
scanf("%s", &temp);
strcpy(e1.fname, temp);
scanf("%s", &temp);
strcpy(e1.lname, temp);
scanf("%d", &e1.id);
scanf("%d", &e1.salary);
/* PRINTING EMPLOYEE DETAILS AS OUTPUT */
printf("%s %s %d %d", e1.fname, e1.lname, e1.id, e1.salary);
return 0;
}
(c.)
#include<stdio.h>
int main(){
int counter=0;
for(counter=0; counter<=200; counter+=5){
printf("%d ", counter);
}
return 0;
}
(d.)
#include<stdio.h>
int main(){
int counter=0;
for(counter=200; counter>=0; counter-=5){
printf("%d ", counter);
}
return 0;
}
(e.)
#include<stdio.h>
int main(){
int a[10] = { 2, 5, 7, 9, 4, 6, 11, 10, 3, 8};
for(int i=0; i<10; i++){
if(i<9){
printf("%d ", a[i]+a[i+1]);}
else{
printf("%d", a[i]);
}
}
return 0;
}