Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ help

Name: Anonymous 2012-02-03 11:28

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: Anonymous 2012-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 (1>='a'){
        l=1 - ('a' - 'A');
    }
    if (1<'A'){
        return -1;
    }
    else if(1<'D'){
        return 2;
    }
    else if(1<'G'){
        return 3;
    }
    else if(1<'J'){
        return 4;
    }
    else if(1<'M'){
        return 5;
    }
    else if(1<'P'){
        return 6;
    }
    else if(1<'T'){
        return 7;
    }
    else if(1<'W'){
        return 8;
    }
    else if(1<='Z'){
        return 9;
    }
    else {
        return -1;
    }
}

Name: Anonymous 2012-02-03 11:37

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: Anonymous 2012-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: Anonymous 2012-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

Name: Anonymous 2012-02-03 11:52

do i use a bool?

Name: Anonymous 2012-02-03 12:47

int phoneKey(char letter)
{
    if (!((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z'))) {
        return -1;
    } else {
        letter |= 0x20;
        if (letter <= 'o') {
            return 1 + (letter - 'a') / 3;
        } else if (letter <= 's') {
            return 7;
        } else if (letter <= 'v') {
            return 8;
        } else if (letter <= 'z') {
            return 9;
        }
    }
}

bool leapYear(int year)
{
    if (year % 4 != 0) return false;
    if (year % 100 == 0 && year % 400 != 0)    return false;
    return true;
}

int monthDays(int month, int year)
{
    if (month == 2) {
        return leapYear(year) ? 29 : 28;
    } else {
        static const int monthDays[] = { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 10, 31 };
        return monthDays[month];
    }
}

int dayNumber(int day, int month, int year)
{
    int dayNumber = 0;
    for (; month >= 1; month--)
        dayNumber += monthDays(month, year);
    dayNumber += day;
    return dayNumber;
}

Name: Anonymous 2012-02-03 12:57

that is right anon, but technically we havent learned arrays or for loops yet so i cant use them :(

Name: Anonymous 2012-02-03 15:28

C language is best language. Enjoy your A bro.

#define _XOPEN_SOURCE
#include <ctype.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>

int phoneKey(char l) {
    static int nums[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,8,8,8,9,9,0,0,0,0};
    if(tolower(l) < 'a' || tolower(l) > 'z') return -1;
    else return nums[tolower(l) - 'a'];
}
bool isLeap(int year) {
    return !(year % 4) && (year % 100 || !(year % 400));
}
int dayNumber(char *date) {
    struct tm tt;
    strptime(date, "%B %d, %Y", &tt);
    return tt.tm_yday + 1;
}
/* Alternate year-day parsing provided for jews */
int dayNumberP(int day, int month, int year) {
    static int days[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    return day + days[month - 1] + (isLeap(year) && month > 2);
}

int main() {
    char x;
    size_t i;
    for(x = 'a'; x <= 'z'; x++)
        printf("%c => %d, %c => %d\n", x, phoneKey(x), toupper(x), phoneKey(toupper(x)));
    static int testYears[] = {1920, 1946, 2000, 2400, 2100, 2341, 2101};
    for(i = 0; i < sizeof testYears / sizeof *testYears; i++)
        printf("Year %d is %sa leap year.\n", testYears[i], isLeap(testYears[i]) ? "" : "not ");
    static char* testDays1[] = {"January 1, 1994", "December 31, 1993", "December 31, 1996"};
    static int testDays2[] = {1, 1, 1994, 31, 12, 1993, 31, 12, 1996};
    for(i = 0; i < sizeof testDays1 / sizeof *testDays1; i++)
        printf("Date %s is day %d\n", testDays1[i], dayNumber(testDays1[i]));
    for(i = 0; i < sizeof testDays2 / (3 * sizeof *testDays2); i++)
        printf("Date %s is day %d\n", testDays1[i], dayNumberP(testDays2[3*i], testDays2[3*i+1], testDays2[3*i+2]));
    return 0;
}

Name: Anonymous 2012-02-03 16:26

>>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: Anonymous 2012-02-03 16:50

help my dubs

Name: Anonymous 2012-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: Anonymous 2012-02-03 20:09

>>10
An array and an array element are two distinct things you stupid shit.

Name: Anonymous 2013-09-01 14:31



                              ,. '"´ ̄ ̄ ̄ ̄ ̄`ヽ
          ト、      /:!         ,'
         _.」::r`ヽ.--‐ァ'/!::!_          !    気. 私 べ
 (.,,___  ,. '"´  '、'、.,__!ヽ.//__// `ヽ.      |    に. は  っ
  、.,_ ,'    、ー->r'-ァ-ェ'イ___,.  !       i   し  全 別
    !   ゝ‐ァコ-'"´   `ヽーヽーrヘ       i   て  然 に
    r'ーァ'´ /  、」__,./!、 ;  i.  `ヾ_ハ     !   な    い
   ノY    ;'  /_」_/,.| / !、ハ_  ; i-'     !   い     い
    `!   ノ! ,'/´;' ri`レ' レァt、!`ハ ノノ    ∠   か.    の
グスッ !  ! .レ'i ヘ. ゝ-'  ..::::. じリ!イイ(      !   ら    よ
    ノ ,' ゚ o 。!''゚   ____'  '' !゚o iン ポロ   ',
  / ノ__,,.イ|  ト、   '、__ソ  ,ハ   ',   ポロ  `''ー-----------‐ '
 ,'  ,.'"ァ'´`'ー-っ', `i. 、,__,.,.</ ! _ ト、              、| l || lll l || ll |l l
ノ! / ;'    ,r'! !イ、___/ i  ハ´:::`ヽ.ヽ.      く7_ノ) ヽ`
 ', i  ;'   ,.イi/ /ゝ、}>く{ ,.イ! /::::::::::::::::::':、)く{ ,,. -‐ァ'ム´  ニ  マ  ま
 ノ !i7   ム レ'ヽ、/ムヽ、/レ':::::::::::::::::::::::::ヽ./:::<Oi      三  ジ  さ
'  r/   ハ>iく{:::::::::/!:::::::::}>!く{:::::::::::::::::::::::::::: i、::::::::::;'      Ξ  泣   か
  ';'    i'7〈〉:::::::::;'::;::::::::::::〈〉'、::::::::::`>'"´`ヾ7、:::::::i      ニ   き  の
ヽ、!   ,イ:!:::::::::::::::、:::::::::::::::::::::ノ>-へ.,r_'´`´ヽ7:::::!      三  ! ?  
 ノ`ァー'´ ノ`i':::::::::::::::::::::::::::::::´イ,.'-‐''"´       ノ:::;'      彡,
,' (  イ r7ヽr-、:;_」_;;::-‐ァー'7    _,,.. -‐i'"´ヾ、        '/l | ll |l |l | ll |l |l

Name: Anonymous 2013-09-01 16:03



     , '´     _{ r-、_ ト、,__ノ }`ヽ /|
 く \{ {  r─'"`     `ゝ、.,_,,.イ-<.,<.,
 r,>- 、アー'´    /      、  ヽ_} | ヽ.,」
  {r‐ァ' ,   / /  ;  |!  ,|   ハ   \ヽ,ハ、
  ヽ7 ./  ;' '  /| .| | /.ト-‐ァ'|!    |`ヽ_}
   | |   | -|-イ_ !__」 レ_,斗テ7T  ;'  |  |
   、 、 ム,斗テ7i'      乂__ソハ_/  ,   !
    \」_ 爪 乂_,ソ      =''"| \_/l|  |
      | ハ`'=    .      ""|  | |  |
      | 八""    _     .|  | |  |
      |  |l ゝ、      ,. イ.|     |  |
      |  |  |`≧;ァーrァ'升 /!.   |、 |
      |  |  ||  __,>人/  |    ; \.|
      |  |  ;'/´}  |/   |    リ  ヽ
      八 '、 /;  }⌒X⌒{  八  /_   ハ
     }>}二{K{  /7_/ハ_,「  }>}三K_{    }
     //  |/ ./ }7 /  }ヽ  /  ハ   _,rム

Don't change these.
Name: Email:
Entire Thread Thread List