06/04/2015

Programs 02

Use only the functions discussed in these pages.

Do NOT Use functions not discussed in these pages.

Sum Problems (continued)

  1. Write a C program with file name s7.c that gets a positive integer from the user and puts it in variable top. It then finds and prints the sum of the first top positive numbers: (1+2+3+...+top). For example, if the user's input is 5, then program should find 1+2+3+4+5 and print the result: 15. It should print only the final result.
  2. Write a C program with file name s8.c. It should get a positive integer from the user and store it in the variable, cutoff. Then it should find and print the sum of all positive odd numbers less than the cutoff. For example if the user's number is 11, then the program should add (1+3+5+7+9) and print the result 25. Once again the program should print only the final result.
  3. Write a C program with file name s9.c. It should get a positive integer from the user and store it in the variable, numOdds. Then it should find and print the sum of that many odd numbers. For example if the user's number is 4, then the program should add (1+3+5+7) and print the result 16. Once again the program should print only the final result.
  4. Write a C program with file name s10.c. a positive integer from the user and store it in variable increment. It should find and print the sum of 100 numbers. The first number is 1. Each new number is larger than the last by the user's number. Example: Suppose the user's number is 3. The the sum will start:
    1+4+7+10+13+ ...
    Once again the program should print only the final result.
    
  5. Write a C program with file name s11.c. It should get a positive integer from the user and store it in variable start. It should find and print the sum of consecutive positive numbers from start up to 100:
       start + (start+1) + (start+2) + ... + 99 + 100
    
    Once again the program should print only the final result.

String Problems

  1. Write a C program with file name str1.c. This program will get a string from the user and print out its length. That's the number of characters in it. The zero at then end does not count. We will have inside main:
      char str[100];
      fgets(str, 100, stdin);
    
    Now we want a loop like the loop in the last example in section 3. We don't need to print the characters. We are more interested in the index,-- the number in the brackets.
  2. Write a C program with file name str2.c. This program will put two strings together to make one long string. The first thing it does is set up an array big enough to hold both strings:
      char str[100];
    
    Then it uses fgets(str, 100, stdin) to get the first string at the beginning of the array. The first argument is str since str is the symbol for the start of the array. Suppose the user enters the string DOG. So stored in the first five places we have
    'D' 'O' 'G' '\n' 0
                     ^ str+4
                 ^ str+3
             ^ str+2
         ^ str+1
    ^ str
    
    Now we want to use fgets again to get the second string.
      fgets(??, ??, stdin);
    
    We want the second string to start right after the end of the first. Technically the newline character code, '\n', is part of the string, but to the user the last meaningful thing he or she typed was the G. Thus we want the second string to start at the address now containing the newline character code, '\n'. So the second fgets now looks like
      fgets(str+3, 100-3, stdin);
    
    Note the second argument is 100-3, since we've used up three positions in the array so there are only 97 positions left. So the second fgets will put the new string right after the G. This works fine if the first string has 3 characters. But if the first string has say, 5 characters before the newline then the second fgets would be:
      fgets(str+5, 100-5, stdin);
    
    So to handle any string that might be entered, you are going to have to use a variable:
      int nlPos;
      //get the position of the newline into variable, nlPos
      fgets(str+nlPos, 100-nlPos, 100-stdin);
    
  3. Write a C program with file name str3.c. This program will get a string from the user and print it out in reverse order. For example if the user enters CAT, the program will print TAC.
  4. Write a C program with file name str4.c. This program will get a string from the user and print it out several times, each time with one less character from its front. For instance if the input string is goodbye, then the program should print:
    goodbye
    oodbye
    odbye
    dbye
    bye
    ye
    e
    

Pattern Problems (continued)

  1. Line. Write a C program with file name pat4.c that gets a number from the user and then prints a horizontal line with that many asterisks.
  2. Line. Write a C program with file name pat5.c that gets a number from the user and then prints a vertical line with that many asterisks.

Big Data Problems

  1. Write a C program with file name bd1.c. This program expects input to be a file of decimal integers. It is to find the sum of the input integers. It is to print only one value: the final total.
  2. Write a C program with file name bd2.c. This program expects input to be a file of decimal integers. It is to count the number of input integers greater than or equal to 100. It is to print only one value: the final count.
  3. Write a C program with file name bd3.c. This program expects input to be a file of decimal integers. It is find the sum of all input numbers that are less than 100. It is to print only one value: the final sum.
  4. Write a C program with file name bd4.c. This program expects input to be a file of decimal integers. It is to find the largest number in the file. It should print only this number. Be careful: Suppose all the numbers are negative.