Project: Files and Nested Loops

Pick one of the following assignments below and complete it. Each assignment is worth a different amount of points. However, you must get it working perfectly to receive the specified number of points, versions that don't work will only receive partial credit.

Each assignment requires reading data from a file, and making some report about it. The harder assignments will require some sort of nested loop, and ones toward the end will have several levels of nesting.

You cannot get credit for two different programs, so doing a 60-point project and also a 100-point project won't get you 160 points, but only 100.

However, since harder projects are just the easier problems with modifications to make them more difficult, it might be a good idea to start with the easiest project you're certain you can complete quickly, and then work your way up the list from there.

For each project, I'll provide a file or two you can use to test it, as well as show you what the correct output is supposed to be.

Choices

  1. (30 points) You will be given a file containing a list of numbers. The first line of the file will be a count of how many numbers are in the file. Print out the sum of all the numbers in the file (except, of course, for the number in the first line).

    Which file would you like to read numbers from: 4proj30a.txt
    This file seems to contain 10 numbers...
    The sum is 377
    
  2. (35 points) You will be given a file containing a list of words. The first line of the file will be a count of how many words are in the file. Print out the number of times the word "the" appears.

    Which file would you like to read words from: 4proj35a.txt
    This file seems to contain 291 words...
    The word "the" occurs 19 times.
    
  3. (38 points) You will be given a file containing a list of words. The first line of the file will be a count of how many words are in the file. The second line of the file will be a word to count. Print out the number of times the given word appears (not counting the one on the second line, of course).

    Which file would you like to read words from: 4proj38a.txt
    This file seems to contain 291 words...
    Looking for the word "a"...
    The word "a" occurs 9 times.
    
  4. (43 points) You will be given a file containing text. Print out the total number of characters in the file.

    Which file would you like to read from: 4proj43a.txt
    This file contains 1442 characters.
    
  5. (45 points) You will be given a file containing text. Print out the number of times the lowercase letter 'e' occurs, as well as the total number of characters in the file.

    Which file would you like to read from: 4proj45a.txt
    The letter 'e' occurs 166 times.
    This file contains a total of 1442 characters.
    
  6. (50 points) You will be given a file containing text. Print out the total number of vowels (both upper and lowercase). Vowels are "a", "e", "i", "o", "u". Though "y" sometimes acts as a vowel, don't count it for this assignment. Also display the total number of characters in the file.

    Which file would you like to read from: 4proj50a.txt
    This file contains 521 vowels.
    This file contains a total of 1442 characters.
    
  7. (55 points) You will be given a file containing text. Print out a table of how many times each vowel occurs, and the total number of characters in the file. Count both upper and lower case together for each vowel.

    Which file would you like to read words from: 4proj55a.txt
    The letter 'a' occurs 118 times.
    The letter 'e' occurs 178 times.
    The letter 'i' occurs 87 times.
    The letter 'o' occurs 107 times.
    The letter 'u' occurs 31 times.
    This file contains a total of 1442 characters.
    
  8. (60 points) You will be given a file containing a list of numbers. Print out a list of how many numbers end in a given digit (use modulus). Use only a single loop.

    Which file would you like to read numbers from: 4proj60a.txt
    This file contains a total of 301 numbers.
    32 numbers ended in a 0.
    25 numbers ended in a 1.
    33 numbers ended in a 2.
    33 numbers ended in a 3.
    29 numbers ended in a 4.
    36 numbers ended in a 5.
    24 numbers ended in a 6.
    25 numbers ended in a 7.
    39 numbers ended in a 8.
    25 numbers ended in a 9.
    
  9. (65 points) You will be given a file containing a list of numbers. Print out a list of how many numbers end in a given digit (use modulus). Use a nested loop (with the "file reading" loop on the inside), and use only a single if statement inside the inner loop.

    Which file would you like to read numbers from: 4proj65a.txt
    32 numbers ended in a 0.
    25 numbers ended in a 1.
    33 numbers ended in a 2.
    33 numbers ended in a 3.
    29 numbers ended in a 4.
    36 numbers ended in a 5.
    24 numbers ended in a 6.
    25 numbers ended in a 7.
    39 numbers ended in a 8.
    25 numbers ended in a 9.
    
  10. (70 points) You will be given a file containing a list of numbers. Display a list of the numbers divisible by everything from 2 to 20. Use a nested loop (with the "file reading" loop on the inside).

    Which file would you like to read numbers from: 4proj70a.txt
    Numbers divisible by 2
      88 366 592 138  84 588 444 730  98 144 684 938 358 470 674 744 154 558 208 304
     194 116 592 626 576 288 886 964 416 860 572 288 712 962 996 562 888 456 562 154
     290 204 898 676 892 276 916  92 976 846   0
    Numbers divisible by 3
     366 837 138  84 117 459 588 903 444 144 213 684 744 558 165 129 417 417 576 288
     489 735 489 129 219 288 573 996 888 456 111 204 969 969 891 633 276 846 987   0
    Numbers divisible by 4
      88 592  84 588 444 144 684 744 208 304 116 592 576 288 964 416 860 572 288 712
     996 888 456 204 676 892 276 916  92 976   0
    Numbers divisible by 5
     730 470 165 125  85 735 860 875 895 290 965   0
    Numbers divisible by 6
     366 138  84 588 444 144 684 744 558 576 288 288 996 888 456 204 276 846   0
    Numbers divisible by 7
     637  84 588 903  98 938 154 329 735 875 154  49 679 889 987   0
    Numbers divisible by 8
      88 592 144 744 208 304 592 576 288 416 288 712 888 456 976   0
    Numbers divisible by 9
     837 117 459 144 684 558 576 288 288 891 846   0
    Numbers divisible by 10
     730 470 860 290   0
    Numbers divisible by 11
      88 253 154 165 121 572 979 407 154 891   0
    Numbers divisible by 12
      84 588 444 144 684 744 576 288 288 996 888 456 204 276   0
    Numbers divisible by 13
     637 117 767 208 299 416 533 572 962 793 676   0
    Numbers divisible by 14
      84 588  98 938 154 154   0
    Numbers divisible by 15
     165 735   0
    Numbers divisible by 16
     592 144 208 304 592 576 288 416 288 976   0
    Numbers divisible by 17
     459  85 799 204 969 969   0
    Numbers divisible by 18
     144 684 558 576 288 288 846   0
    Numbers divisible by 19
     684 304 456 969 969   0
    Numbers divisible by 20
     860   0
    
  11. (75 points) You will be given a file containing a list of numbers. Use nested loops to compute all the products of every pair of numbers from 2 to 25. For each product, search through the file to determine if each number might be a product. Then display the count of matching products for each pair of factors. Put the "file reading" loop on the inside.

    Which file would you like to read numbers from: 4proj75a.txt
    Looking for 2x2 = 4: 1 times.
    Looking for 2x3 = 6: 4 times.
    Looking for 2x4 = 8: 2 times.
    Looking for 2x5 = 10: 5 times.
    Looking for 2x6 = 12: 2 times.
    Looking for 2x7 = 14: 4 times.
    Looking for 2x8 = 16: 3 times.
    Looking for 2x9 = 18: 5 times.
    Looking for 2x10 = 20: 1 times.
    // skip a few...
    Looking for 25x20 = 500: 5 times.
    Looking for 25x21 = 525: 4 times.
    Looking for 25x22 = 550: 3 times.
    Looking for 25x23 = 575: 5 times.
    Looking for 25x24 = 600: 5 times.
    Looking for 25x25 = 625: 0 times.
    
  12. (80 points) You will be given a file containing a list of numbers. Use nested loops to compute all the products of every triple of numbers from 2 to 25. For each product, search through the file to determine if each number might be a product. Then display the count of matching products for each trio of factors. Put the "file reading" loop on the inside. This may take a few minutes to run.

    Which file would you like to read numbers from: 4proj80a.txt
    Looking for 2x2x2 = 8: 1 times.
    Looking for 2x2x3 = 12: 4 times.
    Looking for 2x2x4 = 16: 0 times.
    Looking for 2x2x5 = 20: 3 times.
    Looking for 2x2x6 = 24: 0 times.
    Looking for 2x2x7 = 28: 1 times.
    Looking for 2x2x8 = 32: 1 times.
    Looking for 2x2x9 = 36: 1 times.
    Looking for 2x2x10 = 40: 2 times.
    // skip a few...
    Looking for 25x25x20 = 12500: 0 times.
    Looking for 25x25x21 = 13125: 1 times.
    Looking for 25x25x22 = 13750: 1 times.
    Looking for 25x25x23 = 14375: 2 times.
    Looking for 25x25x24 = 15000: 1 times.
    Looking for 25x25x25 = 15625: 2 times.
    
  13. (85 points) You will be given a file containing a list of numbers. Put the "file reading" loop on the outside. Read a number, and then for each number, use nested loops to determine a pair of numbers from 1 to 500 that multiply to make this number.

    Which file would you like to read numbers from: 4proj85a.txt
    347=
            1x347, 347x1, 
    478=
            1x478, 2x239, 239x2, 478x1, 
    466=
            1x466, 2x233, 233x2, 466x1, 
    276=
            1x276, 2x138, 3x92, 4x69, 6x46, 12x23, 23x12, 46x6, 69x4, 92x3, 138x2, 2
    76x1, 
    203=
            1x203, 7x29, 29x7, 203x1, 
    232=
            1x232, 2x116, 4x58, 8x29, 29x8, 58x4, 116x2, 232x1, 
    418=
            1x418, 2x209, 11x38, 19x22, 22x19, 38x11, 209x2, 418x1, 
    // skip a few...
    423=
            1x423, 3x141, 9x47, 47x9, 141x3, 423x1, 
    123=
            1x123, 3x41, 41x3, 123x1, 
    443=
            1x443, 443x1, 
    102=
            1x102, 2x51, 3x34, 6x17, 17x6, 34x3, 51x2, 102x1, 
    0=
    
  14. (90 points) You will be given a file containing a list of numbers. Put the "file reading" loop on the outside. Read a number, and then for each number, use nested loops to determine a triple of numbers that multiply to make this number. Start checking at one, but only count up to the minimum number necessary to catch all possible factors (hint: itself). This may take a minute or two to run.

    Which file would you like to read numbers from: 4proj90a.txt
    17=
            1x1x17, 1x17x1, 17x1x1, 
    802=
            1x1x802, 1x2x401, 1x401x2, 1x802x1, 2x1x401, 2x401x1, 401x1x2, 401x2x1,
    802x1x1, 
    616=
            1x1x616, 1x2x308, 1x4x154, 1x7x88, 1x8x77, 1x11x56, 1x14x44, 1x22x28, 1x
    28x22, 1x44x14, 1x56x11, 1x77x8, 1x88x7, 1x154x4, 1x308x2, 1x616x1, 2x1x308, 2x2
    x154, 2x4x77, 2x7x44, 2x11x28, 2x14x22, 2x22x14, 2x28x11, 2x44x7, 2x77x4, 2x154x
    2, 2x308x1, 4x1x154, 4x2x77, 4x7x22, 4x11x14, 4x14x11, 4x22x7, 4x77x2, 4x154x1,
    7x1x88, 7x2x44, 7x4x22, 7x8x11, 7x11x8, 7x22x4, 7x44x2, 7x88x1, 8x1x77, 8x7x11,
    8x11x7, 8x77x1, 11x1x56, 11x2x28, 11x4x14, 11x7x8, 11x8x7, 11x14x4, 11x28x2, 11x
    56x1, 14x1x44, 14x2x22, 14x4x11, 14x11x4, 14x22x2, 14x44x1, 22x1x28, 22x2x14, 22
    x4x7, 22x7x4, 22x14x2, 22x28x1, 28x1x22, 28x2x11, 28x11x2, 28x22x1, 44x1x14, 44x
    2x7,44x7x2, 44x14x1, 56x1x11, 56x11x1, 77x1x8, 77x2x4, 77x4x2, 77x8x1, 88x1x7, 8
    8x7x1, 154x1x4, 154x2x2, 154x4x1, 308x1x2, 308x2x1, 616x1x1, 
    175=
            1x1x175, 1x5x35, 1x7x25, 1x25x7, 1x35x5, 1x175x1, 5x1x35, 5x5x7, 5x7x5,
    5x35x1, 7x1x25, 7x5x5, 7x25x1, 25x1x7, 25x7x1, 35x1x5, 35x5x1, 175x1x1, 
    // skip a few
    748=
            1x1x748, 1x2x374, 1x4x187, 1x11x68, 1x17x44, 1x22x34, 1x34x22, 1x44x17,
    1x68x11, 1x187x4, 1x374x2, 1x748x1, 2x1x374, 2x2x187, 2x11x34, 2x17x22, 2x22x17,
     2x34x11, 2x187x2, 2x374x1, 4x1x187, 4x11x17, 4x17x11, 4x187x1, 11x1x68, 11x2x34
    , 11x4x17, 11x17x4, 11x34x2, 11x68x1, 17x1x44, 17x2x22, 17x4x11, 17x11x4, 17x22x
    2, 17x44x1, 22x1x34, 22x2x17, 22x17x2, 22x34x1, 34x1x22, 34x2x11, 34x11x2, 34x22
    x1, 44x1x17, 44x17x1, 68x1x11, 68x11x1, 187x1x4, 187x2x2, 187x4x1, 374x1x2, 374x
    2x1, 748x1x1, 
    0=
            
    
  15. (95 points) You will be given a file containing a list of numbers. For each number in the file, display it's prime factorization, in ascending order. Assume that each number will have no more than 7 factors. It is okay to display several ones at the beginning if the prime factorization contains less than 7 factors.

    Which file would you like to read numbers from: 4proj95a.txt
    560 = 1x2x2x2x2x5x7
    9800 = 2x2x2x5x5x7x7
    3150 = 1x2x3x3x5x5x7
    173 = 1x1x1x1x1x1x173
    38 = 1x1x1x1x1x2x19
    // skip a few
    1400 = 1x2x2x2x5x5x7
    7 = 1x1x1x1x1x1x7
    209 = 1x1x1x1x1x11x19
    22 = 1x1x1x1x1x2x11
    84 = 1x1x1x2x2x3x7
    0 = 
    
  16. (100 points) You will be given a file containing a list of numbers. For each number in the file, display it's prime factorization, in ascending order. Assume that each number will have no more than 7 factors. Don't display any ones unless the number is prime. This is hard.

    Which file would you like to read numbers from: 4proj100a.txt
    560 = 2x2x2x2x5x7
    9800 = 2x2x2x5x5x7x7
    3150 = 2x3x3x5x5x7
    173 = 1x173
    38 = 2x19
    // skip a few
    1400 = 2x2x2x5x5x7
    7 = 1x7
    209 = 11x19
    22 = 2x11
    84 = 2x2x3x7
    0 = 
    



©2013 Graham Mitchell

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Creative Commons License