Homework help

This is a discussion on Homework help within the C Programming forums, part of the General Programming Boards category; I am a 16-year old Filipino who is a 2nd year college student at Informatics Computer Institute (Malolos, Bulacan branch). ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Homework help

    I am a 16-year old Filipino who is a 2nd year college student at Informatics Computer Institute (Malolos, Bulacan branch). I have a coursework assignment in CS215 (C Programming) and I need your extensive help. Please post the following codes so that I only need to worry about the compilation.

    Here are the requirements for the assignment:

    1. Do not add any nonsense in my C code, for my lecturer will not accept any nonsense added in my code!!!
    2. Be able to demonstrate and explain programs to your lecturer.
    3. Submit a physical file holding the following:
    a) Printed hard copy of all programs
    b) Printed test cases
    c) All soft copy programs in a CD-R (please attach firmly to
    physical file)
    4. Do what is exactly asked in the questions.

    The assignment is worth 40 points.
    The deadline is July 25, 2008, so you better hurry up.

    Question 1 (24 points)

    Question 1 contains 12 sub-questions which will be in separate C programs. I'll be the one who will design the filenames for each program.

    Each of the functions below needs to be run in a program. Each of the programs should have at least one test case (with a screen capture) which shows the succesful running of the program. Each sub-question is worth 2 points.

    a) Write a function called ChangeToNext that takes in an uppercase character parameter ch. The function returns the next uppercase character if ch is between 'A' and 'Y'. If ch == 'Z', the function returns 'A'. You may assume that the character parameter ch is always an uppercase alphabetic character.

    b) Write a function called Difference that takes in an integer array intarr of size 10. The function returns the difference between the sum of all values from cell 0 to 4 and the sum from cell 5 to 9. For example, if intarra[10]={1,1,1,1,1,2,2,2,2,2}, Difference (intarr) returns -5 ((1+1+1+1+1) - (2+2+2+2+2)

    c) Write a function PerimeterMinusCenter that takes in a two-dimensional array called intarr of 3 rows and 3 columns. The function sums the values in the perimeter of the array. It then returns the difference of this sum and the center value (the value in cell row 1, column 1 or [1][1]).

    For example, if
    Code:
    intarr[3][3] = {1,1,1}
                                    {1,2,1}
                                    {1,1,1}
                                    };
    PerimeterMinusCenter(intarr) returns 6 (1+1+1+1+1+1+1+1 - 2).

    d) Write a function Compare that takes in a two-dimensional array called intarr of 3 rows and 3 columns. The function sums up separately the values in the diagonal of the array and the non-diagonal values. It then compares these two sums and return 1 if the diagonal sum is larger than the non-diagonal sum or 0 otherwise.

    For example, if
    Code:
    intarr[3][3] = {{1,2,2}
                                      {2,1,2}
                                      {2,2,1}
                                      };
    Compare(intarr) returns 0 (since (1+1+1) < (2+2+2+2+2+2)).

    e) Write a recursive function called CountEvenASCII that takes in a string parameter str and return the number of characters with even ASCII codes. For example CountEvenASCII("ABCDE") returns 2 (as B and D are of even ASCII codes).

    f) Change your solution above (sub-question e) into an iterative solution.

    g) Write a function called ReturnNextCase that takes in a character parameter ch and returns the next alphabet in the other case. For example, ChangeCase('A') returns 'b' and ChangeCase('a') returns 'B'. You may always assume the character parameter ch is always alphabetic.

    h) Write a function called ReturnASCIIDifference that takes in a string parameter str. The function sums up separately even ASCII characters and odd ASCII characters and return their difference. For example, ReturnASCIIDifference('ABCD') returns 2 (as the ASCII values of 'A', 'B', 'C', and 'D' are 65, 66, 67, and 68 respectively and (66+68) - (65+67) = 2).

    i) Write a function called CheckPosNeg that takes in a reference integer parameter num. The function returns 0 if the content of the reference parameter is a positive number and 1 otherwise.

    j) Write a function called PrintToZero that takes in a reference integer parameter num. The function prints out the content of num up to zero. For example, if *num=10, then PrintToZero(num) prints 10 9 8 7 ... 0..
    Last edited by mkdl750; 07-03-2008 at 09:56 PM. Reason: additional info

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    lol, read the rules.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Sorry, sir, I'm just a noob and I need you help. Please post the codes if necessary.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    But it's your assignment? Read http://cboard.cprogramming.com/annou...t.php?f=4&a=39

    What happens when you finish your course (if you do...)? They're testing you, not us. If you have a specific question, feel free to ask. ie, part 'a':

    Code:
    char ChangeToNext(char ch)
    {
        /* insert code here */
    }
    ... and what's to say we don't contact the institute and tell them you're cheating? It's only fair. Not to mention 3 weeks is plenty of time.
    Last edited by zacs7; 07-03-2008 at 10:27 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    If you put as much effort into your programming as you do with your presentation and sucking up skills, then you might actually get somewhere.

    http://www.catb.org/~esr/faqs/smart-....html#homework
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Okay sorry about that I'll be the one to design the codes myself and need further help later on.

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    requesting obligatory posting of mac's signature and lock... in that order.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Code:
    #include <stdio.h>
    char ChangeToNext(char ch);
    main()
    {
        char x;
        printf("Enter uppercase letter: ");
        fflush(stdin);
        scanf("&c",&x);
        if (x == 'A' || x == 'B' || x == 'C' || x == 'D' || x == 'E' || x == 'F' ||
        x == 'G' || x == 'H' || x == 'I' || x == 'J' || x == 'K' || x == 'L' ||
        x == 'M' || x == 'N' || x == 'O' || x == 'P' || x == 'Q' || x == 'R' ||
        x == 'S' || x == 'T' || x == 'U' || x == 'V' || x == 'W' || x == 'X' ||
        x == 'Y' || x == 'Z')
        {
            ChangeToNext(x);
        }
        else
        {
            printf("\nWrong character. Try again.\n");
        }
        getch();
        return 0;
    }
    char ChangeToNext(char ch)
    {
        printf("\nThe next alphabet letter is: ");
        if (ch == 'A')
        {
            printf("B");
        }
        else if (ch == 'B')
        {
            printf("C");
        }
        else if (ch == 'C')
        {
            printf("D");
        }
        else if (ch == 'D')
        {
            printf("E");
        }
        else if (ch == 'E')
        {
            printf("F");
        }
        else if (ch == 'F')
        {
            printf("G");
        }
        else if (ch == 'G')
        {
            printf("H");
        }
        else if (ch == 'H')
        {
            printf("I");
        }
        else if (ch == 'I')
        {
            printf("J");
        }
        else if (ch == 'J')
        {
            printf("K");
        }
        else if (ch == 'K')
        {
            printf("L");
        }
        else if (ch == 'L')
        {
            printf("M");
        }
        else if (ch == 'M')
        {
            printf("N");
        }
        else if (ch == 'N')
        {
            printf("O");
        }
        else if (ch == 'O')
        {
            printf("P");
        }
        else if (ch == 'P')
        {
            printf("Q");
        }
        else if (ch == 'Q')
        {
            printf("R");
        }
        else if (ch == 'R')
        {
            printf("S");
        }
        else if (ch == 'S')
        {
            printf("T");
        }
        else if (ch == 'T')
        {
            printf("U");
        }
        else if (ch == 'U')
        {
            printf("V");
        }
        else if (ch == 'V')
        {
            printf("W");
        }
        else if (ch == 'W')
        {
            printf("X");
        }
        else if (ch == 'X')
        {
            printf("Y");
        }
        else if (ch == 'Y')
        {
            printf("Z");
        }
        else if (ch == 'Z')
        {
            printf("A");
        }
        else
        {
            printf("\nError.");
        }
        return;
    }
    Here's sub-question A. I have to display the next uppercase letter of the alphabet (in this case 'Z' has to roll back to 'A') and everytime i finish my input, I get a "Wrong character" message. Any further tweaks on this one?

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    I almost forgot the "scanf("&c",&x);" in line 8 is actually "scanf("&#37;c",&x);". Thank you for your consideration in subquestion A. I'm advancing to subquestion B.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Here is subquestion B.

    I have to write a function called "Difference" that takes in an integer array "intarr" of size 10. The function returns the difference between the sum of all values from cell 0 to 4 and the sum from cell 5 to 9. For example, if intarra[10]={1,1,1,1,1,2,2,2,2,2}, Difference(intarr) returns -5 ((1+1+1+1+1) - (2+2+2+2+2). Any valuable hints?

    Here's the start of my code:

    Code:
    #include <stdio.h>
    void Difference[int intarr[10]
    main()
    { //code goes here
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Any valuable hints?
    Yes. You need to read your textbook and/or notes again concerning function syntax. For example,
    Code:
    void Difference[int intarr[10]
    should be the function prototype:
    Code:
    void Difference(int intarr[10]);
    The "10" is optional in this case, since it will be ignored by the compiler. But note that you declared the function's return type as void, but your example has the function returning -5. Clearly, there is an inconsistency there.

    Also, the global main function returns an int, so you should explicitly define it as such. You should also explicitly return 0 at the end of the global main function, though this is optional with respect to the 1999 edition of the C standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Here is your First solution

    Code:
    #include "stdio.h"
    char ChangeToNext(char ch);
    int main(int argc, char* argv[])
    {
    
      char ch; 
      char Next_ch;
      printf("enter the character\n");
      scanf("&#37;c", &ch);
      Next_ch= ChangeToNext(ch);
      printf("%c", ch);
    	return 0;
    }
    char ChangeToNext(char ch)
    {
      if(ch>'Y'&&ch<'N')
       {
         return ++ch;
        }
        else if (ch=='N')
        {
          return 'Y';
        }
    }
    Last edited by laserlight; 07-04-2008 at 12:48 AM. Reason: "Corrected" the code.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    shwetha_siddu, do not post homework solutions unless it is clear that the member has sufficiently attempted to solve the question. To this end, I have modified your code example so that it will not produce the correct answer.

    EDIT:
    Actually, come to think of it, your code is incorrect to begin with since your ChangeToNext() implementation has one control path that does not return a value. Oh well.
    Last edited by laserlight; 07-04-2008 at 12:49 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    > mkdl, this is a really not a good way to do this.
    What I do suggest is to use char() - e.g: char(65), output: A
    Something like this:
    Code:
    char GetNext(char a)
    {
     if(a == 'Z')
        return 'A';
     else
     {
      int dec = (int)a + 1;
      return char(dec);
      }
    }
    About the other assignments; these things are really basic and shouldn't take long, just go over what you were taught in class (strings library functions, casting, loops etc')

    Well, I was kinda bored so...
    e)
    Code:
    int rec(char *str)
    {
     if(str[0] == '\0')
    	return 0;
     if((int)str[0] &#37; 2 == 0)
    	return 1 + rec(str + 1);
     else
    	return rec(str + 1);
    }
    The iterative solution is very basic, if you are still having troubles, look for strlen.


    EDIT: If I'm overriding a rule, please edit my post, thanks.
    Last edited by eXeCuTeR; 07-04-2008 at 03:10 AM.

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by mkdl750 View Post
    Sorry, sir, I'm just a noob and I need you help. Please post the codes if necessary.
    http://www.ctrlaltdel-online.com/comic.php?d=20060823
    ...cause everything is about definitions....

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21