program looping with final output

This is a discussion on program looping with final output within the C Programming forums, part of the General Programming Boards category; Hi, I have made the program below loop for a set number of times, in this case 5 times. On ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    program looping with final output

    Hi,
    I have made the program below loop for a set number of times, in this case 5 times.
    On each pass, it creates an output series of coordinates: (2, 4, 5) for example.
    Then it allows you to make another series based on a new set of inputs.
    I would like the program to store each set of outputs and their corresponding inputs, and display these after the loop has been run the number of times it was asked to.

    I believe this must involve creating new variables for each of the outputs.
    But how do I generate these (recursively?) if I allow the user to decide how many times the loop will run, and therefore how many output sets will be created?

    Thanks.

    Code:
    #include <iostream>
    
    int main (void) {
    
        
    /* Variables */
    	float Xreal; 
    	float Yreal; 
    	float Zreal;
    	float Xforce;
    	float Yforce;
    	float Zforce;
    	float Tval;
    	char inZforce[64];
    	
    	int check_input;
    	char input_buffer[160];
    	
    	int wloop = 5;
    
    	
    /* Prompts */
    	
    	/* Loop */
    	while (wloop > 0)
    	{
    	wloop--;
    	/* Input part 1 */
    	printf("Real-Point values in (x, y, z) ");
    	fgets(input_buffer, 160, stdin);
    	check_input = sscanf(input_buffer, "(%f,%f,%f)", &Xreal, &Yreal, &Zreal);
    	while (check_input !=3) {
    		//Improper format
    		printf("Improper format.");
    		return(0);
    	};
    	
    	
    	
    	/* Input part 2 */
    	printf("Zforce value: ");
    	fgets(inZforce, 64, stdin);
    	Zforce=strtof(inZforce, NULL);
    	
    	
    /* Computation */
    	Tval=Zforce/Zreal;
    	Xforce=Tval*Xreal;
    	Yforce=Tval*Yreal;
    	
    /* Output Display */
    	printf("The point: (%.3f, %.3f, %.3f) was generated with a Tvalue of: %.3f.\n",Xforce,Yforce,Zforce,Tval);
    	}
    	printf("Complete.\n");
    	return(0);
    }

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Perhaps an array of Points which are structures themselves with members x and y, and these structures contain the member of your zFource inputed values?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    I changed the code so that the number of repetitions would be based upon a user input, we'll call each of these "a vertex."
    It seems to me that the program should then generate the appropriate number of variables which will each hold a string value containing the output information for the corresponding vertex.

    I'm not sure if it possible in this manner though.
    can i have the program generate say:
    char vertex_1[64];
    char vertex_2[64];
    ...etc
    char vertex_n

    where n represents the number of vertices generated by the number inputted by the user?

    Thanks!
    Code:
    /* Loop */
    	printf("How many vertices would you like to calculate: ");
    	fgets(inwloop, 8, stdin);
    	wloop=atoi(inwloop);
    	printf(" \n");
    	
    	
    	while (wloop > 0)
    	{
    	wloop--;

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    "Recursively" is the wrong term.

    You can create a fixed number of arrays of your input string, and also your x, y, z and computed values. Then, loop through them. Something along the lines of this...

    Code:
    #define DEPTH 5 
    
    int main (void) {
    
       
    	/* Variables */
    	float Xreal[DEPTH] ; 
    	float Yreal[DEPTH] ; 
    	float Zreal[DEPTH] ;
    	float Xforce[DEPTH] ;
    	float Yforce[DEPTH] ;
    	float Zforce[DEPTH] ;
    	float Tval[DEPTH];
    	char inZforce[DEPTH][64];
    	
    	int check_input;
    	char input_buffer[DEPTH][160];
    .
    .
    .
    And then wrap your logic up in another loop that processes DEPTH times.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Thanks!
    Two questions:
    1) instead of defining DEPTH with a set value, can I make DEPTH a variable and have the user set this number?

    2)Using DEPTH, how do I access each level of depth (for lack of a better term)? That is, how do I set it up such that at the end I can display the output values for each cycle?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    To do it dynamically requires a bit different approach, and an associated increase in complexity.

    A good way to do it would be to define a structure with all your values, and then for every loop, malloc() a new structure. You'll also need a dynamic list of pointers to keep track of the dynamically obtained structures.

    To access the element using DEPTH, create an outer loop, sort of like this...

    Code:
    for (j = 0 ; j < DEPTH ; j++) 
    { 
         your current code... , using name[j] instead of just "name"...
    }
    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Hi,
    So I've changed the code so that there are a set number of loops, in this case 8 (determined by variable "wloop").
    This means I can establish a set number of variables to each hold the value for the output of one time through the loop.

    I need 8 variables:
    char force-vertex_0
    char force-vertex_1
    char force-vertex_2
    ...etc
    char force-vertex_7

    and at the end of each loop I need the program to set a value for one of those.
    But how do I get the loop to increase the vertex-variable number it is setting?
    In other words, I need to do something like:
    force-vertex_J=wloop - 8 (and then remove the "negative" from this difference)
    so that when:
    wloop =8, j=0
    wloop =7, j=1
    ...
    wloop =2, j=6
    wloop =1, j=7

    How do I go about this?
    First, what procedure will work? and secondly, how do I drop the negative.

    Below is my code.
    At the end of the loop, I've tabbed out an area for the storage procedure.

    Thanks for your help!


    Code:
    #include <iostream>
    
    int main (void) {
    
        
    /* Variables */
    	float Xreal; 
    	float Yreal; 
    	float Zreal;
    	float Xforce;
    	float Yforce;
    	float Zforce;
    	float Tval;
    	char inZforce[64];
    	
    	int check_input;
    	char input_buffer[160];
    	
    	int wloop = 8;
    
    	
    /* Prompts */
    	
    /* Loop */
    	while (wloop > 0)
    	{
    	wloop--;
    	
    	
    	/* Get real */
    	printf("Real-Point values in (x, y, z) ");
    	fgets(input_buffer, 160, stdin);
    	check_input = sscanf(input_buffer, "(%f,%f,%f)", &Xreal, &Yreal, &Zreal);
    	while (check_input !=3) {
    		//Improper format
    		printf("Improper format.");
    		return(0);
    	};
    	
    	
    	
    	/* Get Z coordinate of force */
    	printf("Zforce value: ");
    	fgets(inZforce, 64, stdin);
    	Zforce=strtof(inZforce, NULL);
    	
    	
    /* Computation */
    	Tval=Zforce/Zreal;
    	Xforce=Tval*Xreal;
    	Yforce=Tval*Yreal;
    	
    /* Output Display */
    	printf(" \n");
    	printf("Forced Point: (%.3f, %.3f, %.3f) \n",Xforce,Yforce,Zforce);
    	printf("Real Point:   (%.3f, %.3f, %.3f) \n",Xreal,Yreal,Zreal);
    	printf("T-value: %.3f \n",Tval);
    	printf(" \n");
    	
    /* Save vertex value in storage */
    	/* STORAGE PROCEDURE HERE */
    	
    	} /* End Loop */
    
    	
    /* Final Display */	
    	printf("Vertex creation complete. \n");
    	/* Then display Force-Vertex 0-7 coords */
    	
    	
    	return(0);
    }

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    By "drop the negative" I mean give the absolute value. (sorry)
    J= | wloop - 8 |

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    so that when:
    wloop =8, j=0
    wloop =7, j=1
    ...
    wloop =2, j=6
    wloop =1, j=7
    j = 8 - wloop ;

    Code:
    	int wloop = 8;
    
    	
    /* Prompts */
    	
    /* Loop */
    	while (wloop > 0)
    	{
    	wloop--;
    shuould be written as
    Code:
    for(wloop = 7; wloop >= 0; wloop--)
    fix your indentation

    Code:
    fgets(inZforce, 64, stdin);
    write as
    Code:
    fgets(inZforce, sizeof inZforce, stdin);
    so that you will not make an error in case when the size of the array changed
    Also reading code without magic numbers is easier
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    J=abs(wloop-8);
    forcevertex_J=("Forced: (&#37;f,%f,%f), Real%f,%f,%f)", &Xforce, &Yforce, &Zforce, &Xreal, &Yreal, &Zreal);


    something like that, so that forcevertex_0, forcevertex_1, ..., forcevertex_7 will correspond to each loop.
    But I understand I can't do it in exactly that manner because forcevertex_J=... will set a value for a variable which is actually called "forcevertex_J"

    Thanks

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    do not use variables with names like forcevertex_J
    use array
    forcevertex[J]
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    right. of course, don't need abs. just 8 - wloop. thanks
    but what about making the "forcevertex_J"?

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    How do I bracket the loop you established:
    "for(wloop = 7; wloop >= 0; wloop--)"
    meaning, do I still keep the "{" after the above?

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by hebali View Post
    How do I bracket the loop you established:
    "for(wloop = 7; wloop >= 0; wloop--)"
    meaning, do I still keep the "{" after the above?
    yes the same
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    and I'm not sure if I understand how to implement the array:
    within the loop, I should call:
    j = 8 - wloop
    forcevertex[j] = ("Forced: (&#37;f,%f,%f), Real%f,%f,%f)", &Xforce, &Yforce, &Zforce, &Xreal, &Yreal, &Zreal);

    in the variables list, do I simply add
    "forcevertex0", "forcevertex1", etc. "forcevertex7"
    will that work?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling an external program + capture output?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 12:49 AM
  2. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM
  3. Ideas for FINAL program?
    By ray in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2002, 02:46 PM
  4. My menu program won't stop looping
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21