Assignment: Moving values between files and integer to digit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ken62310
    New Member
    • Oct 2006
    • 2

    #1

    Assignment: Moving values between files and integer to digit

    1. write a program that tests the value (between 1 and 12 ) of an int variable num1 (take the value from the a file). If the value is 10, square num1.If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. In all the other cases, print ++num1. (take the value from a file and write the results into a file)

    2. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of -2345 as 2 3 4 5.

    I NEED HELP ><!!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What don't you know how to do.

    Comment

    • ken62310
      New Member
      • Oct 2006
      • 2

      #3
      Originally posted by Banfa
      What don't you know how to do.
      in the first one, i don't know how to take the value from a file and put the value to a file.
      in the second one, how to get each digit of the integer...

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Here is a sample for 1

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        
        int main(int argc, char **argv)
        {
        	FILE *fin;
        	FILE *fout;
        
        	fin = fopen("input.txt", "r");
        
        	if (fin != NULL)
        	{
        		char line[250];
        		int value;
        
        		fgets(line, sizeof line, fin);
        
        		value = atoi(line);
        
        		fclose(fin);
        
        		fout = fopen("output.txt", "w");
        
        		if (fout != NULL)
        		{
        			fprintf(fout, "%d\n", value);
        			fclose(fout);
        		}
        		else
        		{
        			puts("Failed to open output file");
        		}
        	}
        	else
        	{
        		puts("Failed to open input file");
        	}
        
            return 0;
        }
        It reads 1 number from the first line of a file.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          and here is an example for the second

          Code:
          #include <stdio.h>
          #include <limits.h>
          
          void InterativePrintDigits(int value);
          void RecursivePrintDigits(int value);
          
          int main(int argc, char **argv)
          {
          	int value = 24807635;  /* This is a random number */
          
          	InterativePrintDigits(value);
          	RecursivePrintDigits(value);
          	printf("\n");
          
              return 0;
          }
          
          void InterativePrintDigits(int value)
          {
          	int mask = 1000000000;  /* You could calculate this value */
          	int firstNonZeroFound = 0;
          	int digit;
          
          	/* get the maximum digit */
          	while(mask)
          	{
          		digit = value / mask;
          
          		if (firstNonZeroFound || digit)
          		{
          			printf("%d ", digit);
          			firstNonZeroFound = 1;
          		}
          
          		value %= mask;
          		mask /= 10;
          	}
          
          	printf("\n");
          }
          
          void RecursivePrintDigits(int value)
          {
          	if (value == 0)
          	{
          		return;
          	}
          
          	RecursivePrintDigits(value/10);
          
          	printf("%d ", value%10);
          }

          Comment

          • ladyrose1990programmer
            New Member
            • Oct 2007
            • 1

            #6
            Originally posted by Banfa
            What don't you know how to do.

            I don't know this program

            Comment

            Working...