Question

(Write a program, and show the output too) C programming I have a hard time to...

(Write a program, and show the output too) C programming

I have a hard time to get these five questions done. I am thankful if someone can help me with that.

I started with something but i couldn't get it done.

That's what I've done.

#include <stdio.h>
typedef struct _profile {
   char name[32];
   int age;
} Profile;

Profile prof[3] = {"myName", 19}, {"bob", 12}
arr[1] = prof;


for (int i = 0; i < 3; i++)
{
   printf('%s', arr[i].name);
}

Consider this

struct _profile {

char name [32] ;

int age;

}

That's the assignment.

Use the above structure

put all the below questions in one main file, compile and run, show the output.

11. Define a array of structures with three cells using the above structure. Name the array Profile ;

12. Assign the cell at index 0 : name = "Cindy", age = 28;

Assign the cell at index 1 : name = "Sean", age = 38;

Assign the cell at index 2 : name = "John", age = 23;

13. print the values of all structures using a for loop.

14. Define a array of structures with three cells using the above structure. Name the array ProfileBackup [ 3 ] ;

Copy all three cells from Profile array to ProfileBackup using a for loop.

15. Search and print the index of the cell where you could find "John".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

11. Define a array of structures with three cells using the above structure. Name the array Profile ;

// array of structures 3 cells
   Profile prof[3];

12. Assign the cell at index 0 : name = "Cindy", age = 28;

Assign the cell at index 1 : name = "Sean", age = 38;

Assign the cell at index 2 : name = "John", age = 23;

// assign the value to it
   strcpy(prof[0].name , "Cindy");
   prof[0].age = 28;
   strcpy(prof[1].name ,"Sean");
   prof[1].age = 38;
   strcpy(prof[2].name , "John");
   prof[2].age = 23;

13. print the values of all structures using a for loop.

// print the prof data
   for (i = 0; i < 3; i++)
   {
   printf("Name is : %s, Age is : %d\n", prof[i].name,prof[i].age);
   }

14. Define a array of structures with three cells using the above structure. Name the array ProfileBackup [ 3 ] ;

Copy all three cells from Profile array to ProfileBackup using a for loop.

   // copy backup data
   Profile ProfileBackup[3];
   for (i = 0; i < 3; i++)
       ProfileBackup[i] = prof[i];

15. Search and print the index of the cell where you could find "John".

// finding the index of John
   for (i = 0; i < 3; i++)
   {
       if(strcmp(ProfileBackup[i].name,"John") == 0)
           printf("The index of John is : %d\n",i);
   }

Complete Code:

#include <stdio.h>
#include <string.h>
typedef struct _profile {
char name[32];
int age;
} Profile;
int main()
{
   int i;
   // array of structures 3 cells
   Profile prof[3];
  
   // assign the value to it
   strcpy(prof[0].name , "Cindy");
   prof[0].age = 28;
   strcpy(prof[1].name ,"Sean");
   prof[1].age = 38;
   strcpy(prof[2].name , "John");
   prof[2].age = 23;
     
     
   // print the prof data
   for (i = 0; i < 3; i++)
   {
   printf("Name is : %s, Age is : %d\n", prof[i].name,prof[i].age);
   }
  
   // copy backup data
   Profile ProfileBackup[3];
   for (i = 0; i < 3; i++)
       ProfileBackup[i] = prof[i];
      
      
   // finding the index of John
   for (i = 0; i < 3; i++)
   {
       if(strcmp(ProfileBackup[i].name,"John") == 0)
           printf("The index of John is : %d\n",i);
   }
   return 0;
}

Output:

Name is Cindy. Age is 28 Name is Sea Age is 38 Name is John, Age is 23 The index of John is 2

Add a comment
Know the answer?
Add Answer to:
(Write a program, and show the output too) C programming I have a hard time to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Structures in C language programming

    Write code to accomplish each of the following:Define a structure called Dove containing int variable dovNo and char array dovArr with values that may be as long as 25 characters (including the terminating null character).Define dove to be a synonym for the type struct Dove.Use Dove to declare variable a to be of type struct Dove, array b[ 10 ] to be of type struct Dove and variable ptr to be of type pointer to struct Dove.Read a dovNo and a dovArr from the keyboard into the individual members of variable a.Assign the member values of variable a to element 3 of array b.Assign the address...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT