Question

Ques) Write a program in c, which meets the following requirements.

Requirements 1. Read integer values from stdin, separated by one or more spaces or newlines, until reaching EOF 2. The input4. Input 15 16 15 12 12 12 3 19 21 17 15 12 10 Output 5. Input 2 Output

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

Please find the hash graph program below,

Step 1:  userInputStream api uses File stdio stream to fetch the user input other EOF. It allocated heap memory dynamically for the user input values.

Step 2: strtok api is used to parse all the integer values stored in a string which is collected from Step 1.

Step 3: Rows and columns of a hash graph is collected from number of elements in num array and largest element in the num array.

Step 4: As per guidance from question, traverse the row and compare the element present in

num array and row value to print "#" else print " "

Program

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/* Max input values */

#define MAX_LENGTH 80

/*This api uses File stdio stream to fetch the user input other EOF

and returns as a string*/

char *userInputStream(FILE* fp, size_t size){

char *str;

int ch;

size_t len = 0;

str = realloc(NULL, sizeof(char)*size);//size is start size

if(!str)return str;

while(EOF!=(ch=fgetc(fp))){

str[len++]=ch;

if(len==size){

str = realloc(str, sizeof(char)*(size+=16));

if(!str)return str;

}

}

str[len++]='';

return realloc(str, sizeof(char)*len);

}

int main(void){

char *usrStr;

char* token;

int k=0,i=0,num[MAX_LENGTH]={0,},j=0, column=0,row=0, largest=0, l=0;

  

printf("Enter the input string : ");

usrStr = userInputStream(stdin, 10);

//printf("%s ", usrStr);

/* Initialize all the 80 integers with -1 */

for(i=0; i<MAX_LENGTH; i++)

num[i]=-1;

  

i=0;

  

/* use Strtok to parse all the user input based on space and

new line characters which is stored as integers in num array*/

token = strtok(usrStr, " ");

while (token!= NULL) {

num[i++]=atoi(token);

token = strtok(NULL, " ");

}

  

/* Number of elements in the array represents the column */

column=i;

  

for (i = 0; i < MAX_LENGTH; i++)

{

//printf("-%d-",num[i]);

if(num[i] != -1)

{

if (largest < num[i])

{

largest = num[i];

}

}

}

/* Largest element in an array is row of the graph */

row=largest;

  

//printf(" %d %d ",row, column);

printf(" ");

  

/* As per guidance from question, traverse the row and compare the element present in

num array and row value to print "#" else print " " */

for(j=row; j >0 ; --j)

{

l=0;

for(k=0; k<column; k++)

{

if(num[l] >= j)

{

printf("#");

}

else

{

printf(" ");

}

l++;

}

printf(" ");

}  

/* free the dynamically allocated heap memory */

free(usrStr);

return 0;

}

Sample Output:

Enter the input atring: 4 12nter the input string341 0 9 7

Add a comment
Know the answer?
Add Answer to:
Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values...
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
  • Read an arbitrary number of positive integer values from stdin. The values are separated by one...

    Read an arbitrary number of positive integer values from stdin. The values are separated by one or more spaces or newlines (only). The input is guaranteed to be well-formed. On standard output, render a simple graph representation of the input values, in order, using hash # characters as shown in the examples below. The number of hashes printed should be equal to the input value. Your program should output exactly one line per input value. The value zero should generate...

  • C Programming Write a program that meets the following requirements: • Your program will read three...

    C Programming Write a program that meets the following requirements: • Your program will read three integer values from the keyboard. • Your program will output the following data in this order: 1. The third divided by the first if the first is not zero. 2. The third divided by the second if the first is zero and the second is not zero. 3. The sum of the first, second and third values. • All numbers are integers and only...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • Write a C# program (Integer Math Calculator, the programming requirements are as follows: When typing in...

    Write a C# program (Integer Math Calculator, the programming requirements are as follows: When typing in 3+4, 10-5, 12*12, 15/5 from console, the program should give the right answers on screen like 7, 5, 144, 3. This is what I have so far: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { String input; do { Console.Write("Type int values to calulate or stop to exit: "); input = Console.ReadLine(); if (input.ToLower() != "stop") { char[] delimiters = {...

  • Write a program in C that meets the following requirements: 1. Your program will read n...

    Write a program in C that meets the following requirements: 1. Your program will read n + 1 integers from the keyboard where n is the first integer read. 2. If n is less than 1 print an error message and return a status of 1. 3. If there are no positive numbers, print “no positive numbers”. This is not an error. 4. If there are positive numbers, print the average of the positive numbers. 5. 0 is neither positive...

  • Write a new program (hw6-pr1) meeting at least the following minimum requirements: Opens the text version...

    Write a new program (hw6-pr1) meeting at least the following minimum requirements: Opens the text version of this file (hw6-Spring2017.txt) and searches thru the file for the first occurrence of each of the 26 uppercase characters of the alphabet (A-Z), then each the 10 digits (0-9), and finally each of the 26 lowercase characters of the alphabet (a-z). As it finds each of these characters it should also record its position in the file (assume the first character in the...

  • this is a C++ program! You will write a program that performs the following tasks for...

    this is a C++ program! You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not opened, enter into a loop that prints out an error message, resets the input file stream variable (see the hints section), obtains a new file...

  • C++ program by netBeans java language Exercise #2: Write a java program that prompts the user...

    C++ program by netBeans java language Exercise #2: Write a java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, 0, o, I, i) c. the number of characters as numbers or special characters (other than English letters a..z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse:...

  • C++ requirements All values must be read in as type double and all calculations need to...

    C++ requirements All values must be read in as type double and all calculations need to be done using type double. For part 2 you MUST have at least 4 functions, including main. The main function will be the driver of the program. It needs to either do the processing or delegate the work to other functions. Failure to follow the C++ requirements could reduce the points received from passing the tests. General overview This program will convert a set...

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