Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except...

#include <stdio.h>
#include <stdlib.h>

#define K   1024

/** (2pts)
* This problem is like p1, except that you should read the number using scanf()
* and the string to print using fgets() (up to 1024 characters.) Use "num: " as
* the prompt for the number and "str: " as the prompt for the string. Keep in
* mind that the newline that is entered will still be in the string when
* printing it. NOTE: After the scanf() for the number, you will need to
* a) use fgets() to "eat" the newline that follows the number, so use fgets once
*    (and discard the input) before printing the second prompt and reading the
*    string.
* or:
* b) Use scanf and a %c to "eat" the newline character following the number. You
*   can do this in the same scanf that reads the number.
* Example:
* > ./p2
* num: 3
* str: Hello
*   1 Hello
*   2 Hello
*   3 Hello

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

#define K 1024

/** (2pts)
* This problem is like p1, except that you should read the number using scanf()
* and the string to print using fgets() (up to 1024 characters.) Use "num: " as
* the prompt for the number and "str: " as the prompt for the string. Keep in
* mind that the newline that is entered will still be in the string when
* printing it. NOTE: After the scanf() for the number, you will need to
* a) use fgets() to "eat" the newline that follows the number, so use fgets once
* (and discard the input) before printing the second prompt and reading the
* string.
* or:
* b) Use scanf and a %c to "eat" the newline character following the number. You
* can do this in the same scanf that reads the number.
* Example:
* > ./p2
* num: 3
* str: Hello
* 1 Hello
* 2 Hello
* 3 Hello
*/

int main()
{
char str[K];
int i, num;
  
// read integer
printf("num: ");
scanf("%d",&num);

fgets(str,K,stdin);// read and discard \n left by scanf

// read string
printf("str: ");
fgets(str,K,stdin);

// loop num times, displaying integers from 1 to num followed by str (containing the newline)
for(i=1;i<=num;i++)
printf("%d %s",i,str);
return 0;
}

//end of program

Output:


Related Solutions

Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */ void printArray(int *arr, int size){ int i; for( i = 0; i < size; i++) { // Print each element out printf("%d ", *(arr+i)); //Print addresses of each element printf("%p", (arr+i)); //Printing a new line printf("\n"); } } int main() { // Allows user to specify the original array size, stored in variable n1. printf("Enter original array size: "); int n1 = 0; scanf("%d",...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a .c file that does the following: 1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7) • Note: The parent should save the pid_t of the child (returned from fork()) for later. 2. Have the child register a...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <stdlib.h> // The below function Merges two subarrays of arr[]. // First subarray...
#include <stdio.h> #include <stdlib.h> // The below function Merges two subarrays of arr[]. // First subarray is arr[low..mid]--left to middle // Second subarray is arr[mid+1..high]--middle+1 element to right most element void merge(int arr[], int low, int mid, int high) { int i, j, k; int len1 = mid - low + 1; int len2 = high - mid; //create two temporary arrays int A[len1], B[len2]; /* Copy data to temporary arrays A[] and B[] */ for (i = 0; i...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT