Question

c program Your teacher want to find out who is the most hardworking student in class!...

c program

Your teacher want to find out who is the most hardworking student in class! They want to input the names according to the order they fell asleep, then print their names in the reverse order. Although you can create an array large enough to store all names, your teacher don’t want to waste our precious memory!

The first line of the input is an integer x, which indicate the number of students in class. Then there are x lines of strings. Each line corresponds to a student’s name. Assume a name is at most 14 characters.

Task 1: Read the students’ names from user input and pass the data to your teacher’ function. You are requested to create an array with x strings. Your teacher have given you the skeleton code. Please complete the main function to achieve your task.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void foo(char **names, int x)

{

int i;

for (i = x - 1; i >= 0; i--)

     printf(“%s\n”, names[i]);

}

int main(void)

{

// write your codes here (use fgets() to read input, why??)

foo(names, x);

// write your codes here

return 0;

}

Sample Input:

3

George Bush

Bill Gates

Steve Jobs

Sample Output:

Steve Jobs

Bill Gates

George Bush

Your teacher suddenly realized that the lecture has covered “structure”! So they says: “Let’s change the code and use structure!”

Task 2: You task is the same, but your teacher have given you another (terrible) skeleton code below. Please complete the main function to achieve your task.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

int no;

char(*names)[16];

} myList;

void foo(myList *bar)

{

int i;

for (i = bar->no - 1; i >= 0; i--)

     printf(“%s\n”, bar->names[i]);

}

int main(void)

{

// write your codes here

foo(bar);

// write your codes here

return 0;

}

One of your (annoying) teacher says: “I don’t like the design of the structure ._.”

Task 3: You are given another structure declaration below. Complete the same task as in Task 2.

typedef struct {

int no;

char names[][16];

} myList;

Problem 2 (Memory Layout):

Run the following code snippets using the online compiler or VS, what do you expect?

#include <stdio.h>

int hi(void) {}

int main(void)

{

printf("Hello\n");

int a = hi();

printf("%d\n", a);

}

Explain the output.

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

Please find the code below:

Part one

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void foo(char **names, int x)

{
   int i;
   for (i = x - 1; i >= 0; i--)
       printf("%s ", names[i]);

}

int main(void)

{

   // write your codes here (use fgets() to read input, why??)
   printf("Enter number of students : ");
   int x;
   scanf("%d",&x);

   getch();
   char** names;
   names= (char **)malloc(x* sizeof(char*));
   int i;
   for(i=0;i<x;i++){
       names[i] = (char *)malloc(14* sizeof(char));
   }
   fgets(names[0], 14, stdin); //ingore extra line
   for(i=0;i<x;i++){
       fgets(names[i], 14, stdin);
   }

   foo(names, x);

   // write your codes here

   return 0;

}

output:

Part Two

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

   int no;

   char(*names)[16];

} myList;

void foo(myList *bar)

{

   int i;

   for (i = bar->no - 1; i >= 0; i--)

       printf("%s ", bar->names[i]);

}

int main(void)

{

   // write your codes here (use fgets() to read input, why??)
   printf("Enter number of students : ");
   myList *bar;
   bar = (myList *)malloc(1* sizeof(myList));
   scanf("%d",&(bar->no));
  
bar->names= (char **)malloc(bar->no* sizeof(char*));

   fgets(bar->names[0], 16, stdin); //ingore extra line

   // write your codes here
   int i;
   for(i=0;i<bar->no;i++){
       fgets(bar->names[i], 16, stdin);
   }


   foo(bar);

   // write your codes here

   return 0;

}

output:

Part Three

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

   int no;

   char names[][16];

} myList;

void foo(myList *bar)

{

   int i;

   for (i = bar->no - 1; i >= 0; i--)

       printf("%s ", bar->names[i]);

}

int main(void)

{

   // write your codes here (use fgets() to read input, why??)
   printf("Enter number of students : ");
   myList *bar;
   bar = (myList *)malloc(1* sizeof(myList));
   scanf("%d",&(bar->no));

   fgets(bar->names[0], 16, stdin); //ingore extra line

   // write your codes here
   int i;
   for(i=0;i<bar->no;i++){
       fgets(bar->names[i], 16, stdin);
   }


   foo(bar);

   // write your codes here

   return 0;

}

output:

On running code:

#include <stdio.h>

int hi(void) {}

int main(void)

{

printf("Hello ");

int a = hi();

printf("%d ", a);

}

getting below output:

Add a comment
Know the answer?
Add Answer to:
c program Your teacher want to find out who is the most hardworking student in class!...
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 C programming Using the supplied example code as a starting point, add 3 more conversions...

    Using C programming Using the supplied example code as a starting point, add 3 more conversions using strtod(). Make sure you change the output conversion for type double. Use the same input strings and base codes as the strtol() function example but notice how the numbers are now represented. Manipulate the width and precision as needed for a good presentation.   Supplied code: #include <stdio.h> #include <string.h> int main(void) { long num; char* ptr; num = strtol("12345 Decimal Constant: ", &ptr,...

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf...

    can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format dont worry about the answers i have down Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( char str[ 39764 int N strlen(str) int nunj for (int i-0; i t Nǐ.de+ printf(") num (str[] 'e); I/ digits range in the ASCII code is 48-57 if (nue > e) f 9 printf( Xdin,...

  • CHALLENGE ACTIVITY 5.71 String library functions. Assign the size of userinput to stringSize. Ex: if userinput...

    CHALLENGE ACTIVITY 5.71 String library functions. Assign the size of userinput to stringSize. Ex: if userinput is 'Hello output is: Size of user Input: 5 1 #include <stdio.h> 2 #include <string.h> 4 int main(void) { char user Input[50]; int stringSize; BO scanf("%s", userInput); /* Your solution goes here" printf("Size of user Input: %d\n", stringsize); return ; Run

  • Write a statement that calls a function named IncreaseItemQty, passing the variable addStock. Assign notebookInfo with...

    Write a statement that calls a function named IncreaseItemQty, passing the variable addStock. Assign notebookInfo with the value returned by IncreaseItemQty. #include <stdio.h> #include <string.h> typedef struct ProductInfo_struct {    char itemName[30];    int itemQty; } ProductInfo; ProductInfo IncreaseItemQty (ProductInfo productToStock, int increaseValue) {    productToStock.itemQty = productToStock.itemQty + increaseValue;    return productToStock; } int main(void) {    ProductInfo notebookInfo;    int addStock = 10;    scanf("%s", notebookInfo.itemName);    scanf("%d", &notebookInfo.itemQty);    /* Your solution goes here */    printf("Name:...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • In C Set hasDigit to true if the 3-character passCode contains a digit. 1 #include «stdio.h>...

    In C Set hasDigit to true if the 3-character passCode contains a digit. 1 #include «stdio.h> 2 #include«string.h> 3 #include «stdbool.h 4 #include<ctype.h> 6 int main(void) 7 bool hasDigit; 8 char passCode501; 10 11 12 hasDigit false; scanf("%s", &passcode); 13 Your solution goes here * 14 15 if (hasDigit) 16 printf("Has a digit.n 4 #include <ctype.h> 6 int main (void) 1 8 char passCode [50]; bool hasDigit; hasDigit- false; 10 11 scanf("%s",&passcode); 12 13 14 15 if (hasDigit) 16 17...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

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