week 4 notes

# week 4 notes - Converting a number main (int argc, char *...

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]; { // 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; { 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)

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.

## This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University.

### Page1 / 21

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online