Question: Extend the following program so that it uses getchar ( only ) to read and interpret floating point literals #include #define SIZE 50 // assume

Extend the following program so that it uses getchar (only) to read and interpret floating point literals

#include

#define SIZE 50 // assume no more than 50 literals in input

int main(){

int c;

int value;

int resu[SIZE];

int negative;

int index = 0;

int i;

value = 0;

c = getchar();

while ( c != EOF){

if (c=='-'){

negative=1;

}

else if (c == ' ' || c == ' ')

{

if(negative==1){

value = -value;

}

resu[index] = value;

index++;

value=0;

negative=0;

}

if(c >= '0' && c <= '9')

value = value*10+(c-'0');

c = getchar(); // read next

}

printf("-------- ");

for(i=0;i

printf("%d\t%d ", resu[i],resu[i] *2);

return 0;

}

  • should use getchar() to read inputs (from standard in) until EOF is read. The input contains floating point literals and integer literals separated by one blanks or new line characters. The program interprets each literal and put into an float array. When end of file is reached, the program prints out the value of each float as well as the value multiplied by 2, as shown in sample output below.
  • assume all floating point literals are valid float/double or int literals (both 2.3, 5, .4 are considered valid). There are no 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(), atol(), atof(), strtol(), strtoul(). (So have to convert manually)

Sample Inputs/Outputs: (download dont copy/paste - the input file input2E.txt)

red 306 % a.gcc lab2E.c -o lab2

red 306 % lab2

2.3 4.56

43.3 43 5.3

.3 1.2

^D

--------

2.3000 4.6000

4.5600 9.1200

43.3000 86.6000

43.0000 86.0000

5.3000 10.6000

0.3000 0.6000

1.2000 2.4000

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!