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

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!

2

u/psychosquatch42 Feb 01 '17

Would you mind showing me what that would look like for one line of my code, so that i have a basis to work from, im just beginning in C and im still struggling with the basics, im so sorry.

3

u/[deleted] Feb 01 '17
  1. include <iostream>

  2. include <string>

  3. int main(){

  4. std::string linearray[1] = {"#include <stdio.h>"};
  5. for(int i = 0; i < (sizeof(linearray)/sizeof(*linearray)); i++){
  6.     std::cout << linearray[i] << std::endl;
  7. } 9.}

I typically program in C++, but the basics don't change much between Java, C, C++. So, forgive me if this isn't compiling but it should put you in the right direction.

So, lines 1&2 are including libraries. They include the iostream library and the string library. Both of these libraries have important functions inside of them and are pretty much standard. "iostream" allows you to output and input information (strings, int, objects, doens't matter). "string" allows you to perform manipulations on strings.

Line 4 declares an array and initializes the array. It's saying an array holding "1" elements will be equal to (or store) the element "#include <stdio.h>" which is just a string. If you want to store more variables, you need to increase the size of the array by changing 1 to a bigger number. And then initialize the array with {"element1", "element2", "element3",....};

line 6 is a for loop. For loops have three conditions.

  1. a variable

  2. a condition (<, >, =, !=, ...)

  3. an incremental value

This array says:

  1. An integer called "i" is equal to 0.

  2. i < LENGTH OF THE ARRAY we initialized above. This is weird syntax for a noobie. You could also say "i < 1"

  3. i++ is the same thing as i+=1 or increment i by one value.

Line 7 just outputs the line. This is likely different syntax for C and C++ so look up "how to print an array element in C"

2

u/psychosquatch42 Feb 01 '17

Thank you again for setting aside time to explain this to me, honestly you explain it better than any teacher i have had. I have got a perfect starting point now, thank you so much.

1

u/[deleted] Feb 01 '17

You are welcome. I'm still learning too. It helps me to explain things.