Functions

+1 alfred ofori · June 18, 2015

Purpose: Use functions.

Description:

Write four functions.

The first two functions will translate the parameter passed
to it into integers in the range 0-9.
- The first function will translate the letters 'A'-'J'
to the range 0-9.
- The second will translate the integer values 1-10 into
the range 0-9.

The third and fourth functions will be boolean functions
that test to see if the values passed into them are in a
specific range.
- The third function will be passed a character and will
produce true if it is in the range 'A'-'J'.
- The fourth function will be passed an integer and will
produce true if it is in the range 1-10.

Note:
We are preparing for the game Battleship. The game
board for battleship is a 10x10 grid where rows
are denoted by letters 'A' - 'J', and columns are
indicated by numbers 1 - 10. In C a 10x10 grid
would fundamentally have row and column values
0 - 9.

Have your program read in a character and an integer,
then using your functions print the translated results
in the range of 0-9, or print a message indicating that
a value is not in its expected ranges.

Ex 1:
----
Enter a char: B
Enter an int: 12

'B' translates to 1.
12 is out of input range.

Ex 2:
----
Enter a char: Z
Enter an int: 5

'Z' is out of input range.
5 translates to 4.

Ex 3:
----
Enter a char: B
Enter an int: 10

'B' translates to 1.
10 translates to 9.


===============================================================

Post a Reply

Replies

Oldest  Newest  Rating
0 Alan Johnson · June 18, 2015
Enjoy, I've done your homework haha.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*
- The first function will translate the letters 'A'-'J'
 to the range 0-9.
*/
int translate_a_j(char c) {
return abs('A' - c);
}

/*
- The second will translate the integer values 1-10 into
 the range 0-9.
*/
int translate_1_10(int x) {
return x - 1;
}

/*
- The third function will be passed a character and will
 produce true if it is in the range 'A'-'J'.
*/
int in_range_a_j(char c) {
return c >= 'A' && c <= 'J';
}

/*
- The fourth function will be passed an integer and will
 produce true if it is in the range 1-10.
*/
int in_range_1_10(int x) {
return x >= 1 && x <= 10;
}

int main() {

for (;;) {
char c;
int x;

printf("\nEnter a char: ");
scanf(" %c", &c);

printf("\nEnter an int: ");
scanf("%d", &x);

// Check character
if (in_range_a_j(c)) {
printf("'%c' translates to %d. \n", c, translate_a_j(c));
} else {
printf("'%c' is out of input range. \n", c);
}

// Check number
if (in_range_1_10(x)) {
printf("%d translates to %d. \n", x, translate_1_10(x));
} else {
printf("%d is out of input range. \n", x);
}
}

return 0;
}
0 Jon Z. · June 18, 2015
here

/images/forum/upload/2015-06-18/2a8d635bf4ef4d5469e3a6258e480034.JPG

/*
* checkAndTranslate.c
* 18/June/2015
* by Jon Z. jonz@inbox.xom
*/

#include
#include

int charToInt(char c);
int intToInt(int i);
bool charInRange(char c);
bool intInRange(int i);

main(){
    int inputInt = 0;
    char inputChar = 0;

    printf("Enter 'q' to exit.\n\n");

    while(true){
        printf("Enter char: ");
        scanf(" %c", &inputChar);
        if(inputChar == 'q'){break;} //exit if either input is 'q'

        printf("Enter int: ");
        scanf(" %d", &inputInt);
        if(inputInt == 'q'){break;} //exit if either input is 'q'

        printf("\n");
        //deal with char
        if(charInRange(inputChar)){
            printf("%c translates to %d.\n", inputChar, charToInt(inputChar));
        }
        else{
            printf("%c is out of input range.\n", inputChar);
        }

        //deal with int
        if(intInRange(inputInt)){
            printf("%d translates to %d.\n", inputInt, intToInt(inputInt));
        }
        else{
            printf("%d is out of input range.\n", inputInt);
        }

        printf("\n\n");

    } //end of loop

}

int charToInt(char c){
    c = c-65;
    return c;
}

int intToInt(int i){
    i--;
    return i;
}

bool charInRange(char c){
    if(c < 65 || c > 74){
        return false;
    }
    else{
        return true;
    }
}
bool intInRange(int i){
    if(i < 1 || i > 10){
        return false;
    }
    else{
        return true;
    }
}

  • 1

C

107,060 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator