Thread: Data held concurrently in the memory.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Data held concurrently in the memory.

    this is the requirement
    Up to 100 pit data entries can be held
    concurrently in the laptop memory.
    Entering new pit data:
    1- The user selects 'd' or 'D' from the menu.
    2- The user enters the pit location name.
    3- The program prompts for entry of the time
    for a stone to hit the pit bottom (measured
    using a stopwatch). Several measures can be
    taken in succession.
    4- Only the last depth estimate is recorded to
    memory as the user enter 'n' or 'N' when asked
    whether to take another measure.


    HOW DOES THE DATA GET STORED IN MEMORY?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You put it there, presumably, by writing it to mydata[63] or some such. What exactly is your question about?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    when the user enter 'no', only the last septh estimate should get recorded in the memory.
    how can i do that?
    insert
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    #include <stdlib.h>
    bool yesNo(const char prompt[]);
    double depth(double seconds2Bottom);
    double toBottom;
    char location[80], filename[80];
    int i;
    FILE *fileptr;
    int main(void)
    {
       char choice='j';
       while (choice !=('q'||'Q'))
       {
            printf("\t\t\tMain Menu\n\n");
            printf("(D)epth: Enter pit location, Compute depth	  \n");
            printf("(S)ave : Write pits data to file 	\n");
            printf("(L)oad : Read mine shaft information from file.\n");
            printf("(P)rint: Display mine shaft information.\n");
            printf("(Q)uit : Exit program.\n");
    	    printf("Enter choice(D/S/L/P/Q)\n\n");
            scanf("&#37;c",&choice);
            fflush(stdin);
            switch (choice)
    		{
    		
    			case 'q': case 'Q':
    			   
    			    printf("GOOD BYE\n\n\n");
    				return 'j';
    				break;
    				
    			case 'd': case 'D':
    			   
    		    	do
    				{
    				   printf("\n Enter mine shaft location or name:\n");
    	               fgets(location, 80, stdin);
    		       	   printf(("%s", location);
    				   printf("\nEnter time in seconds to bottom: \n");
    	               scanf("%i ", &toBottom);
    				   fflush(stdin);
    	               printf("\nShaft depth is %.2f m\n", depth(toBottom));
    				}  while( yesNo("Do you want to take another measure"));
    				
    				break;
    		
    			case 's': case 'S':
    		        
    			   printf("\n Enter name of file to enter pit data to:\n");
                   gets (filename);
    		       if((fileptr=fopen(filename, "a"))==NULL)
    		       {
    		       printf("Error: cannot open input file for writing\n");
    			   exit(0);
    		       }	
    			   else   
              
    				break;
    				
    			case 'l': case 'L':
    			
    				printf("Modify selected\n");
    				break;
    				
    			case 'p': case 'P':
    				printf("Backup selected\n");
    				break;
    			default:
    				printf("Invalid selection. Please try again.\n");
    		} /*end of switch*/
    	} /*end of while*/
       
      
     
     /* Pause, then exit */
        printf("Press [Enter] to exit. ");
        getchar();
        return 0;
    }
    
    bool yesNo(const char prompt[])
    {
    	char answer;
    	printf("\n%s (Y/N)? ", prompt);
    	answer = getchar();
    	fflush(stdin);
    	return (answer == 'y' || answer == 'Y');
    }
    
    
    
    double depth(double seconds2Bottom)
    {
    	/* Acceleration of gravity */
    	const double G = 9.81;
    	
    	return -0.5 * G * seconds2Bottom * seconds2Bottom; 
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would request a piece of memory (using, say, malloc) big enough to hold both location and depth, and then write the location and depth to that piece of memory.

    It might be easier to collect your data in a struct that keeps the location and depth together in one object.

    If you know ahead of time some limit on the number of pits you will have, then you can declare an array of those structs, thus getting a bunch of memory all in one shot, meaning writing to the memory would just mean writing to list_of_pits[14] or whatever.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    user enter 'n' or 'N' when asked
    whether to take another measure

    last depth calculated should get stored in memory.
    got struck here.location data will get stored in location string, i know that i need to use parallel arrays but how?
    insert
    Code:
    		  do
        	 {   
    	        printf("\n Enter mine shaft location or name:\n");
    	        fgets(location, 80, stdin);
    			printf(("%s", location);
    	    	printf("\nEnter time in seconds to bottom:\n ");
    	    	fflush(stdin);
    	    	printf("\nShaft depth is %.2f m\n", depth(toBottom));
      	      } while( yesNo("Do you want to take another measure"));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. How to Parallel reading flat file into C ?
    By anwar_pat in forum C Programming
    Replies: 11
    Last Post: 09-16-2006, 09:44 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM