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

Printing Section and Subsection Headings of an Outline You will write a program

ID: 3565507 • Letter: P

Question

Printing Section and Subsection Headings of an Outline

                      

You will write a program which prints Section headings for an outline/document. Let's start with an example:

Section 1

   Section 1.A

       Section 1.A.1

       Section 1.A.2

   Section 1.B

     Section 1.B.1

     Section 1.B.2

Section 2

   Section 2.A

       Section 2.A.1

       Section 2.A.2

   Section 2.B  

       Section 2.B.1

       Section 2.B.2

              

                  

Some observations:

There is a hierarchy; for example:

Section 1 has subsections 1.A and 1.B

Section 2.A has subsections 2.A.1 and 2.A.2

Some sections have no subsections

these are

Section 1

   Section 1.A

       Section 1.A.1

       Section 1.A.2

   Section 1.B

     Section 1.B.1

     Section 1.B.2

Section 2

   Section 2.A

       Section 2.A.1

       Section 2.A.2

   Section 2.B  

       Section 2.B.1

       Section 2.B.2

              

Explanation / Answer

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

//recursive function to print section

//takes parameters for depth to be printed, width, prefix string and the current depth

void print_sec(int d, int w, char pre[],int cur){

    

     //base condition

     if(d<=0)

             return;

     char buf[100];

     int i;

    

     //run loop for w times, for each w subsection of the current depth

     for(i=1; i<=w; i++){

             

              char c;

             

              //subsection numbering based on the current depth level

              if(cur%2==0)

                         c=(char)i+64;

              else

                         c=(char)i+48;

              sprintf(buf, "%s%c", pre, c);

              int j;

             

              //printing spaces for indentation, number of loops depends on the current depth

              for(j=1;j<cur;j++)

                               printf(" ");

                              

              //print current level section

              printf("%s ",buf);

             

              //append a dot to use as prefix for next level

              sprintf(buf, "%s%c.", pre, c);

             

              //call function recursively for next lower level

              print_sec(d-1,w,buf,cur+1);

     }

}

//main function

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

   

    //check no of arguments

    if(argc!=3){

                printf("Invalid no of arguments. Provide two arguments.");

    }

    //convert argument to integers

    int d = atoi(argv[1]);

    int w = atoi(argv[2]);

    if(w<=0 || w>26){

            printf("Width not in range of 1-26. ");

            return;

    }

    if(d<=0 || d>26){

            printf("Depth not in range of 1-10. ");

            return;

    }

    print_sec(d, w, "Section ",1);

}

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