C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2008, 09:26 PM   #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
mkdl750 is offline   Reply With Quote
Old 07-03-2008, 09:32 PM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
lol, read the rules.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 07-03-2008, 10:04 PM   #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.
mkdl750 is offline   Reply With Quote
Old 07-03-2008, 10:09 PM   #4
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 07-03-2008 at 10:27 PM.
zacs7 is offline   Reply With Quote
Old 07-03-2008, 10:50 PM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-03-2008, 11:09 PM   #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.
mkdl750 is offline   Reply With Quote
Old 07-03-2008, 11:19 PM   #7
Registered Abuser
 
Join Date: Jun 2006
Location: Toronto
Posts: 572
requesting obligatory posting of mac's signature and lock... in that order.
@nthony is offline   Reply With Quote
Old 07-04-2008, 12:05 AM   #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?
mkdl750 is offline   Reply With Quote
Old 07-04-2008, 12:13 AM   #9
Registered User
 
Join Date: Jul 2008
Posts: 38
I almost forgot the "scanf("&c",&x);" in line 8 is actually "scanf("%c",&x);". Thank you for your consideration in subquestion A. I'm advancing to subquestion B.
mkdl750 is offline   Reply With Quote
Old 07-04-2008, 12:21 AM   #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
}
mkdl750 is offline   Reply With Quote
Old 07-04-2008, 12:26 AM   #11
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-04-2008, 12:43 AM   #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("%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.
shwetha_siddu is offline   Reply With Quote
Old 07-04-2008, 12:46 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

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

Last edited by laserlight; 07-04-2008 at 12:49 AM.
laserlight is online now   Reply With Quote
Old 07-04-2008, 01:10 AM   #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] % 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.
eXeCuTeR is offline   Reply With Quote
Old 07-04-2008, 02:22 AM   #15
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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....
C_ntua is offline   Reply With Quote
Reply

Tags
assignment, c programming, homework

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Homework kermi3 A Brief History of Cprogramming.com 11 11-03-2001 04:39 PM
Homework kermi3 C Programming 10 09-27-2001 04:49 PM
Homework kermi3 C++ Programming 15 09-26-2001 03:16 PM
Homework kermi3 Windows Programming 5 09-15-2001 11:48 AM
Homework kermi3 C Programming 0 09-10-2001 01:26 PM


All times are GMT -6. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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