Unfortunately, due to it being an online class my teacher never gives us any feedback; he will just grade us based on our results. However, I am actually interested in learning the material and not just getting a good grade. My question is, how do I efficiently do this without writing out 2187 combinations like I did (took my computer 20+ seconds to compile that much code). I felt like I should be using a Multi-dimension array or possibly a struct but my code was not working out right. Yes, Q and Z are removed from the array as requested by the teacher.
#include <stdio.h> // standard input/output library
//function prototypes
int main(void)
{
//declare variables
char phoneNumber[7];// phone number array to convert into a string of alphanumberic characters
char phoneArray[10][3]={{NULL},{NULL},{'A','B','C'},{'D','E','F'},{'G','H','I'},{'J','K','L'},{'M','N','O'},{'P','R','S'},{'T','U','V'},{'W','X','Y'}}; // could not figure out how to properly use this array
char phoneTwo[]={'A','B','C'};
char phoneThree[]={'D','E','F'};
char phoneFour[]={'G','H','I'};
char phoneFive[]={'J','K','L'};
char phoneSix[]={'M','N','O'};
char phoneSeven[]={'P','R','S'};
char phoneEight[]={'T','U','V'};
char phoneNine[]={'W','X','Y'};
int i; //counter for loops
int x;
int j;
FILE *fPtr; // points to file asgn12.out
// opens or creates file - if file doesn't open then show error message
if(( fPtr = fopen( "asgn12.out", "w")) == NULL){
printf("File could not be opened.\n");
} // end if
else{
printf("Input 7 digit phone number\n"); //prompt user
scanf("%s", phoneNumber); //read user's phonenumber
//test phone number for 0's or 1's
for(i=0; i<7; i++){
if( phoneNumber[i] == '0' || phoneNumber[i] == '1'){
printf("Input 7 digit phone number\n"); //reprompt user
scanf("%s", phoneNumber);
}
}
for(i=0; i<7; i++){
if (phoneNumber[i] == '2')
fprintf(fPtr,"%c", phoneTwo[0]);
if (phoneNumber[i] == '3')
fprintf(fPtr,"%c", phoneThree[0]);
if (phoneNumber[i] == '4')
fprintf(fPtr,"%c", phoneFour[0]);
if (phoneNumber[i] == '5')
fprintf(fPtr,"%c", phoneFive[0]);
if (phoneNumber[i] == '6')
fprintf(fPtr,"%c", phoneSix[0]);
if (phoneNumber[i] == '7')
fprintf(fPtr,"%c", phoneSeven[0]);
if (phoneNumber[i] == '8')
fprintf(fPtr,"%c", phoneEight[0]);
if (phoneNumber[i] == '9')
fprintf(fPtr,"%c", phoneNine[0]);}
fprintf(fPtr,"\n");
for(i=0; i<7; i++){
if (phoneNumber[i] == '2')
fprintf(fPtr,"%c", phoneTwo[1]);
if (phoneNumber[i] == '3')
fprintf(fPtr,"%c", phoneThree[0]);
if (phoneNumber[i] == '4')
fprintf(fPtr,"%c", phoneFour[0]);
if (phoneNumber[i] == '5')
fprintf(fPtr,"%c", phoneFive[0]);
if (phoneNumber[i] == '6')
fprintf(fPtr,"%c", phoneSix[0]);
if (phoneNumber[i] == '7')
fprintf(fPtr,"%c", phoneSeven[0]);
if (phoneNumber[i] == '8')
fprintf(fPtr,"%c", phoneEight[0]);
if (phoneNumber[i] == '9')
fprintf(fPtr,"%c", phoneNine[0]);}
fprintf(fPtr,"\n");
for(i=0; i<7; i++){
if (phoneNumber[i] == '2')
fprintf(fPtr,"%c", phoneTwo[2]);
if (phoneNumber[i] == '3')
fprintf(fPtr,"%c", phoneThree[0]);
if (phoneNumber[i] == '4')
fprintf(fPtr,"%c", phoneFour[0]);
if (phoneNumber[i] == '5')
fprintf(fPtr,"%c", phoneFive[0]);
if (phoneNumber[i] == '6')
fprintf(fPtr,"%c", phoneSix[0]);
if (phoneNumber[i] == '7')
fprintf(fPtr,"%c", phoneSeven[0]);
if (phoneNumber[i] == '8')
fprintf(fPtr,"%c", phoneEight[0]);
if (phoneNumber[i] == '9')
fprintf(fPtr,"%c", phoneNine[0]);}
fprintf(fPtr,"\n");
// I repeated this 2187 times to get all 7^3 power results.
fclose(fPtr); //fclose closes file
printf("Check asgn12.out to see results.\n");
}
return 0; //end main
}

New Topic/Question
Reply



MultiQuote



|