Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is written in C . I need help to change the program to where it rea

ID: 3925862 • Letter: T

Question

This program is written in C . I need help to change the program to where it reads from an array instead of a manually inputing it.

#include <stdio.h>
#include <stdlib.h>
#define incharacter(char) char = getchar();
#define outcharacter(char) putchar(char)
#define true 1
#define false 0
#define NL ' '
#define BL ' '
#define MAXPOS 20 // sets the max number

int Alarm;

int main()
{
   int bufpos, fill, CW, k, buffer[MAXPOS+1];
  
   setbuf(stdout,0); //turns off buffering

   Alarm = false;
   bufpos = 0;
   fill = 0;
   do {
  
       incharacter(CW);  
      
          
       if (CW == BL || CW == NL || CW == EOF) {
           if (bufpos != 0) {
               if (fill + bufpos < MAXPOS && fill != 0) {
                   outcharacter(BL);
                   fill = fill +1;
               }
               else {
                   outcharacter(NL);
                   fill = 0;
               }
               for (k=1; k<=bufpos; k++)
                   outcharacter(buffer[k]);
               fill = fill + bufpos;
               bufpos = 0;
           }
       }                  
       else
           if (bufpos == MAXPOS)
               Alarm = true;
           else {
               bufpos = bufpos + 1;
               buffer[bufpos] = CW;
           }
   } while (! (Alarm || CW==EOF) );

                  
   if (Alarm) {
       fprintf(stderr,"ALARM!! "); exit(1);
   }
   else {
       fprintf(stderr,"Everything is normal "); exit(0);
   }
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#define incharacter(char) char = getchar();
#define outcharacter(char) putchar(char)
#define true 1
#define false 0
#define NL ' '
#define BL ' '
#define MAXPOS 20 // sets the max number

int Alarm;

int main()
{
   int bufpos, fill, CW, k, buffer[MAXPOS+1];
  
   setbuf(stdout,0); //turns off buffering

   Alarm = false;
   bufpos = 0;
   fill = 0;
   do {
  
       incharacter(CW);  
      
          
       if (CW == BL || CW == NL || CW == EOF) {
           if (bufpos != 0) {
               if (fill + bufpos < MAXPOS && fill != 0) {
                   outcharacter(BL);
                   fill = fill +1;
               }
               else {
                   outcharacter(NL);
                   fill = 0;
               }
               for (k=1; k<=bufpos; k++)
                   outcharacter(buffer[k]);
               fill = fill + bufpos;
               bufpos = 0;
           }
       }                  
       else
           if (bufpos == MAXPOS)
               Alarm = true;
           else {
               bufpos = bufpos + 1;
               buffer[bufpos] = CW;
           }
   } while (! (Alarm || CW==EOF) );

                  
   if (Alarm) {
       fprintf(stderr,"ALARM!! "); exit(1);
   }
   else {
       fprintf(stderr,"Everything is normal "); exit(0);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote