so i got this c++ engineering assignment and im subpar at it, i know its not hard to do the first function with and array and the second one with true false statements and a for loop but we havent learned either yet so i cant use them, any suggestions?
The functions you are to implement are as follows:
int phoneKey(char l)
A function that returns the digit value (an integer) corresponding to the letter passed to it as an argument based on the encoding on a telephone handset. For example, if the argument is the letter a, b, or c (uppercase or lowercase), your function should return the digit 2. If the argument is not one of the letters of the alphabet, return a value of -1.
int dayNumber(int day, int month, int year)
A function that returns the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1, 1994 is day 1. December 31, 1993 is day 365. December 31, 1996 is day 366 since 1996 is a leap year. Write and use a second function that returns true if its argument, a year, is a leap year. A year is a leap year if it's divisible by four, except that any year divisible by 100 is a leap year only if it's also divisible by 400. (Hint: If x and y are integers and x is divisible by y, then what will be the value of x % y?
help a aspiring mechanical engineer with no desire to do programming out!
Name:
Anonymous2012-02-03 11:30
op here again, heres my current code for the first function, but its not running correctly
int phoneKey(char l){
if(l < 'a' || l > 'z')
return -1;
return (l-'a') / 3 + 1;
}
int leap_year(int year){
return (year % 4 == 0 && year % 100 != 0) || year % 100 == 0 && year % 400 == 0);
}
the dayNumber is just tiresome
Name:
Anonymous2012-02-03 11:40
yeah i know its brutal, you are right but for this shit they are picky and want us to use the same functions given to us, even though that function is perfectly fine
Name:
Anonymous2012-02-03 11:49
so if i was to right a function for dayNumber, do you have any idea what id have to do? Ill take anything
>>8
An array is a location in memory where a fixed amount of elements of the sane size are allocated. For loops are loops that perform an expression, rub the loop, check a predicate, run an iteration then keeps going until the predicate is false. Now go read up on syntax.
Name:
Anonymous2012-02-03 16:50
help my dubs
Name:
Anonymous2012-02-03 18:09
And now in ISO STANDARD C99 #include <stdio.h>
#include <limits.h>
#define TRIPLET(a,b,c,v) [a] = (v), [b] = (v), [c] = (v)
int phoneKey(char c) {
int v = (const int[UCHAR_MAX+1]){
['q'] = 1, ['z'] = 1,
TRIPLET('a','b','c',2),
TRIPLET('d','e','f',3),
TRIPLET('g','h','i',4),
TRIPLET('j','k','l',5),
TRIPLET('m','n','o',6),
TRIPLET('p','r','s',7),
TRIPLET('t','u','v',8),
TRIPLET('w','x','y',9),
['Q'] = 1, ['Z'] = 1,
TRIPLET('A','B','C',2),
TRIPLET('D','E','F',3),
TRIPLET('G','H','I',4),
TRIPLET('J','K','L',5),
TRIPLET('M','N','O',6),
TRIPLET('P','R','S',7),
TRIPLET('T','U','V',8),
TRIPLET('W','X','Y',9)
}[(unsigned char)c];
return v?v:-1;
}
int main(void) {
for (int i = 0; i <= UCHAR_MAX; i++)
printf("%c %d\n", i, phoneKey(i));
}
Name:
Anonymous2012-02-03 20:09
>>10
An array and an array element are two distinct things you stupid shit.