{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

week 4 notes

week 4 notes - Converting a number main(int argc char argv...

This preview shows pages 1–8. Sign up to view the full content.

Converting a number main (int argc, char * argv) convert <basefrom> <baseto> <num> char *num = argv[3]; From: 16 To: 10 Num: BA3 num[0] == ‘B’ d=num[0]*16*16 num[1] == ‘A’ +num[1]*16 num[3] == ‘3’ +num[0] Convert Program Read number then Convert to decimal then Convert to the base required int convertToDecimal(char*num; int base) { int pow =1; int sum =0; int i =0; int n =c-’0’; for(i = 0; i< strlen(num); i++) { char c = num[strlen(num)-1-i]; if(c >= ‘A’ && c <= ‘Z’) { // c is A to Z or a to z n = c -’A’ +10;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
} sum += (n)*pow; return sum; } To Translate decimal to <baseto> use repeated divisions 57 dec to base 16 hexadecimal char numTo[MAXIMUM]; char *convertToBase(int num, int base) { //Use numTo for the converted number // Build Number in reverse using repeated division and then reverse it return numTo; } // To convert a number to a string char str[100]; sprintf(str, “%d”, num); (num%base) ------> remainder operators Convert int convert(char *num, int baseTo) { int i; int pow = 1; int sum = 0; for(i = 0; i < strlen(num); i++){ int j = i - strlen(num)-1;
int c = num[j]; int n; if(c >= ‘0’ && c < = ‘9’) { n = c - ‘0’; } else if (c >= ‘A’) { ...... } } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Notes compiled by Christopher Price TUESDAY CS240: Summer Week Three LAB2 Help Palindromes int isPalindrome(char *s1) { int l = strlen(s); for(i = 0; i<l/z ; i++) { if (s[i] != s[l-i-1]) { return 0; // not a palindrome } } return 1; // it is a palindrome } Take a string and use temporary string and remove the spaces character by character and then apply the previous lines of code #define MAX 512 char tmp1[MAX]; char line[MAX]; void removeSpaces(char *sin, char *sout) { for (int i = 0; i < strlen(sin); i++)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
{ // iterate over all characters in sin if(sin[i] != ‘0’) { sout[j] = sin[i]; j++; } } sout[j] = ‘\0’; // you can also use ‘0’ } main(){ while (gets(line) != NULL){ removeSpaces(line, temp1); if(isPalindrome(temp1)){ } } } You can store the lines into an array of lines char lines [MAX][MAX]; to copy “line” into the ith lines strcpy (lines[i], line); ------------------------------------------------------------------------------------------------------- ----------- TEXT FILES Declared in the header #include <stdio.h> You need to define a file handle to use file FILE *fileHandle;
To open a file fileHandle = fopen(fileName, fileMode); fileMode: r = Open in read only mode w = Open in write only mode a = Open in append mode r+ = Open in both input and output (File must exist) w+ = Open in both input and output (If file does not exit it will create it) a+ = Open in append mode (If file does not exit it will create it) eg.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern