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

Write a program using C that takes four command line arguments: start, stop, ste

ID: 3679077 • Letter: W

Question

Write a program using C that takes four command line arguments: start, stop, step, and file. The first three arguments are nonnegative integers and the fourth is a string. The program is to write numbers to the file specified in the file argument, starting with start, increment by step each time, all the way through stop. Write the numbers ten per line in the file.

Sample Run: Command Prompt$ ./a.out 0 999 20 file.txt

Should produce a file.txt like:

0 20 40 60 80 100 120 140 160 180

200 220 240 260 280 300 320 340 360 380

400 420 440 460 480 500 520 540 560 580

600 620 640 660 680 700 720 740 760 780

800 820 840 860 880 900 920 940 960 980

Explanation / Answer


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

int main( int argc, char *argv[] ) {

   if( argc <5 ) {
      printf("Less number of aruments ");
      return 0;
   }
   int first = atoi(argv[1]);
   int last = atoi(argv[2]);
   int diff = atoi(argv[3]);
   char *filename = argv[4];

   FILE *fp;
   fp = fopen(filename, "w");

   int i;
   int count = 0;
   for(i=first; i<=last; i=i+diff){
       count++;
       fprintf(fp, "%d ", i);
       if(i!=0 && count%10 == 0)
       fprintf(fp, "%s", " ");
       }
   return 0;
}


/*

Output:

0 20 40 60 80 100 120 140 160 180
200 220 240 260 280 300 320 340 360 380
400 420 440 460 480 500 520 540 560 580
600 620 640 660 680 700 720 740 760 780
800 820 840 860 880 900 920 940 960 980

*/

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