Thread: Can some one change this code for me to match the HW assignment?

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    3

    Can some one change this code for me to match the HW assignment?

    I just realized that the directions were a bit more specific than I remembered from class.

    Here's the question:

    1) Write a function findFreq that takes a character array (c-string) and returns the frequency of each
    alphabetic letter. It should have a function heading similar to,
    void findFreq(char text[], char letter[], int frequency[])
    The character array letter is for 26 uppercase letters and int array frequency is to store the
    frequency of letters.
    2) Write a function printFreq that outputs a list of all the letters that occur in the text and the number
    of times each letter occurs. The letters should be listed in alphabetic order. The function printFreq
    should have a function heading similar to,
    void printFreq(char letter[], int frequency[])
    3) Write a function setText that takes a character array and make the following substitutions to the
    letters:
    Change letter ‘A’ -> letter ‘J’,
    letter ‘I’ -> letter ‘Q’,
    letter ‘S’ -> letter ‘Z’.
    You can call function findFreq and printFreq when testing function setText.
    4) The input file hw.txt is provided to you. Your function main should read a C-string from
    the input file and perform several function calls to above user-defined functions.




    Here's what I've been working with:
    I thought the assignment was to JUST make the program output the letter frequency in the text file.


    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
       int freq[128];   
       ifstream inFile;  
       char ch; 
    
    
       inFile.open("C:\\Users\\me\\Desktop\\hw.txt");
    
    
       for (int k = 0; k < 128; k++)
       {
          freq[k] = 0;
       }
    
    
       ch = inFile.get();
       while (ch != EOF)
       {
          cout << ch;
          ch = toupper(ch);
          freq[ch]++;
          ch = inFile.get();
       }
     cout << endl << "Letter:        Frequency:" << endl;
      for (char ch = 'A'; ch <= 'Z'; ch++)
      {
          cout << ch << " :            " << freq[ch] << endl;
      }
        system("pause");
      return 0;
    
    
    }

    I'm not a very good programmer, if someone is up for a challenge, and is in a very generous, helpful mood, I appreciate
    Last edited by Thanase; 02-16-2015 at 03:40 PM.

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    It isn't about being helpful. Anybody that writes the code for you would break the forum rules regarding homework.

    The way this forum works is that you post your code inside code tags, which you have done, and then you ask specific questions. That way board members will help you with nudges until you have a complete solution.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    3
    Oh, okay I gotcha.

    My first question would be, How can I separate the main function into two other functions:

    findFreq: that takes a character array (c-string) and returns the frequency of each

    alphabetic letter.

    and

    printFreq: that outputs a list of all the letters that occur in the text and the number
    of times each letter occurs.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read this FAQ and work on creating the function prototypes.

    http://www.cprogramming.com/tutorial/lesson4.html

    Ask questions on the part you are having issues doing.
    1. creating the function prototypes
    2. writing the functions
    3. calling the functions

    Tim S.
    Last edited by stahta01; 02-16-2015 at 04:32 PM. Reason: Changed URL to C++ one.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    3
    Can an admin please delete this post?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Per normal rules, posts aren't deleted because they contribute knowledge to people seeking help with problems. It makes no sense in deleting such information.
    So why would you want to delete it?
    Also, in case you're still wondering if it can be deleted, the answer is no. No one is going to delete it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with small code change
    By Kryptonit in forum C Programming
    Replies: 4
    Last Post: 08-12-2009, 05:16 AM
  2. very simple code to match letters to numbers
    By kop442000 in forum C Programming
    Replies: 14
    Last Post: 07-24-2009, 05:16 PM
  3. how to change it into one main() function code..
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 12-13-2008, 10:02 PM
  4. Sample C code that change character
    By dianazheng in forum C Programming
    Replies: 11
    Last Post: 05-29-2005, 08:10 AM
  5. Any tool to change c++ code into c code?
    By crliu in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2002, 09:44 AM