if(n == 0)

advertisement
Recursion
Summation
// Return sum of digits in n
// Precond: n >= 0
int sum_digits(int n) {
if(n==0)
return 0;
else return(n%10 + sum_digits(n/10));
}
// Takes in value and base and conducts the recursion here
// and prints after every digit is accounted for
// '\n' is printed in main after function has ended
void convertBase(int value, int base) {
if(value <= 0)
return;
else {
convertBase(value/base, base);
printf("%c", digit(value % base));
}
}
Verifying/Searching
// Return 1 if arr is a palindrome, or 0 otherwise.
// Recursive function
int palindromeRecur(int arr[], int i, int size) {
if(size <= i)
return 1;
if(arr[i] == arr[size])
return palindromeRecur(arr, i+1, size-1);
else return 0;
}
// Find the largest digit pair in n
// Precond: n > 0
int largest_digit_pair(int n, int max) { // max=0 from main
if(n == 0)
return max;
if(n%100 > max) {
max = n%100;
return largest_digit_pair(n/100, max);
}
else return (largest_digit_pair(n/100, max));
}
// Find the min number in an array of n elements
int get_min_index2(int arr[], int size)
{
int index;
if (size==1)
return 0;
index = get_min_index2(arr, size-1);
return arr[size-1] < arr[index] ? size-1 : index;
}
String Handling
// Convert src into pig-latin
void convert(char sen[]) {
char *word;
int len, i;
word = strtok(sen, " ");
while(word != NULL) {
len = strlen(word);
if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u')
printf("%sway ",word);
else {
for(i=1;i<len;i++)
printf("%c", word[i]);
printf("%cay ", word[0]);
}
word = strtok(NULL, " ");
}
printf("\n");
}
Sorting
// Sort points in ascending order of their distance from the origin
void sort(point_t arr[], int size) {
int i, j, m, hypo, alt_hypo;
point_t temp;
for(i=0;i<size-1;i++) {
hypo = (arr[i].x * arr[i].x) + (arr[i].y * arr[i].y);
for(j=i+1,m=i;j<size;j++) {
alt_hypo = (arr[j].x * arr[j].x) + (arr[j].y * arr[j].y);
if(alt_hypo < hypo) {
m = j;
hypo = alt_hypo;
}
}
temp = arr[i];
arr[i] = arr[m];
arr[m] = temp;
}
}
Opening FILES with Command-line Arguments
int main(int argc, char *argv[]) {
student_t students[50];
int numStudents = readStudents(students, argv[1]);
return 0;
}
int readStudents(student_t stu[], char *fileName) {
int numStu = 0, i = 0;
FILE *infile;
infile = fopen(fileName, "r");
while(fscanf(infile,"%s %d", stu[numStu].name, &stud[numStu].numCourses)== 2) {
for (i = 0; i < students[numStu].numCourses; i++) {
fscanf(infile, "%d", &students[numStu].scores[i]);
}
numStu++;
}
fclose(infile);
return numStu;
}
strcmp
int strcmp ( const char * str1, const char * str2 );
Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than
in str2; And a value less than zero indicates the opposite.
Example
1 /* strcmp example */
2 #include <stdio.h>
3 #include <string.h>
4
5 int main ()
6 {
7
char szKey[] = "apple";
8
char szInput[80];
9
do {
10
printf ("Guess my favourite fruit? ");
11
gets (szInput);
12 } while (strcmp (szKey,szInput) != 0);
13 puts ("Correct answer!");
14 return 0;
15 }
strtok
Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
Download