Question
Complete an ANSI-C program, which should use getchar (only) to read and interpret integer. Download the partially implemented file lab2D.c and start from there. The
Complete an ANSI-C program, which should use getchar (only) to read and interpret integer.
Download the partially implemented file lab2D.c and start from there. The program:
-
should use getchar() to read inputs (from standard in) until EOF is read. The input
contains decimal integer literals separated by one blanks or new line characters. The program interprets each literal and put into an int array. When end of file is reached, the program prints out the value of each integer and the double of the value.
-
assume all literals in the input are valid decimal int literals. Note that the input can contain negative numbers.
-
should NOT use other input IO functions such as scanf(), fgets(). Only getChar() is allowed to read input. (So have to read char by char.)
-
should NOT use other library function such as atoi() (So have to convert manually)
input2D.txt:
5 34 534 -12 43 -13 7 54 -122 3245
lab2D.c
#include#define SIZE 50 // assume no more than 50 literals in input int main(){ int c; int value; int resu[SIZE]; int negative; ... c = getchar(); while ( c != EOF){ ... if (c == || c == ) { ... // put current value into array .... } .... c = getchar(); // read next } printf("-------- "); for(i=0, ....) printf("%d\t%d ", resu[i],resu[i] *2); return 0; }
Sample Inputs/Outputs (download - don't copy/paste - the input file input2D.txt)
red 306 % gcc lab2D.c -o lab2D red 306 % lab2D 3 12 435
54 -15 7 98 -10 456 ^D -------- 36 12 24 435 870 54 108 -15 -30 7 14 98 196 -10 -20 456 912 red 308 % lab2D < input2D.txt
-------- 5 10 34 68 534 1068 -12 -24 43 86 -13 -26 7 14 54 108 -122 -244 3245 6490 red 309 %
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started