r/programminghelp • • Feb 01 '17

Help with a C Loop.

So i have to create program with a loop that outputs the program below, im at a loss for where i should start, any help at all would be appreciated.

  #include <stdio.h>
     #define PI 3.141593
     int main()
    {
    int degrees = 0;
    double radians;
    printf("Degrees to Radians \n");
    degrees = 0;
    radians = degrees*PI/180;
    printf("%6i %9.6f \n", degrees, radians);
    degrees = 10;
    radians = degrees*PI/180;
    printf("%6i %9.6f \n", degrees, radians);
    .
    .
    .
    degrees = 360;
    radians = degrees*PI/180;
    printf("%6i %9.6f \n", degrees, radians);
    }
3 Upvotes

6 comments sorted by

View all comments

3

u/[deleted] Feb 01 '17

You want a program that outputs another program?

Just create an array of strings that stores each line. Then, in a for loop std::cout << linearray[i] << std::endl;

Example:

#include <iostream>
int main(){
     std::string linearray[5] = {"hello", "world", "I", "am", "Programming"};

     for(int i = 0; i < 5; i++){
         std::cout << linearray[i] << std::endl;
    }
}

A more C-friendly version:

#include <iostream>
#include <string>
int main(){
     std::string linearray[5] = {"hello", "world", "I", "am", "Programming"};

     for(int i = 0; i < (sizeof(linearray)/sizeof(*linearray)); i++){
         std::cout << linearray[i] << std::endl;
    }
}

2

u/psychosquatch42 Feb 01 '17

oh my god i feel so stupid, thank you so much! That actually makes sense!