CS 2213/1, Spring 2005 Quiz 4 Questions |
Last Name: | First Name: |
Answers in red.
Write a function with prototype void print_string(char[] ); that prints its string parameter one character at a time with a blank after each character. The function should not contain any square brackets ([ or ]). For example, the call print_string("Toad"); should print T o a d .
#include <stdio.h> int main() { char *m[] = {"none","Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec",NULL}; char *w[] = {"none","Mon","Tue","Wed","Thu","Fri","Sat","Sun",NULL}; print_strings(m); print_strings(w); }
By any method write a function print_strings that prints either array of strings above, in each case without the initial string "none", and with a comma and a blank after each string except the last one. print_strings should print a newline at the end. Thus the program above, after you add the function, should print:
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec Mon, Tue, Wed, Thu, Fri, Sat, Sun
#include <stdio.h> void print_strings(char **); int main() { char *m[] = {"none","Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec",NULL}; char *w[] = {"none","Mon","Tue","Wed","Thu","Fri","Sat","Sun",NULL}; print_strings(m); print_strings(w); } | |
void print_strings(char *r[]) { char **p = r+1; while(*p) { /* or *p != NULL */ printf("%s", *p); if (*++p) /* or *++p != NULL */ printf(", "); } printf("\n"); } | void print_strings(char **r) { char **p = &r[1]; printf("%s", *p++); while(*p != NULL) { printf(", "); printf("%s", *p++); } printf("\n"); } |
/* solution by S. Lecollier (corrected) */ void print_strings(char **a) { int i, j; for (i = 1; *(a+i) != NULL; i++) { for (j = 0; *(*(a+i)+j) != '\0'; j++) printf("%c", *(*(a+i)+j)); *(a+i+1) == NULL ? printf("\n") : printf(", "); } } | /* adapted from J. Hammond */ void print_strings(char **a) { while(*++a != NULL) { char *b = *a; while (*b != '\0') printf("%c", *b++); if (*(a+1) != NULL) printf(", "); } printf("\n"); } |
/* adapted from J. Escobar */ void print_strings(char **s) { int i = 0; while(*(s+i) != NULL) { if (strcmp(*(s+i), "none") == 0) i++; else { printf("%s", *(s+i)); i++; if (*(s+i) != NULL) printf(", "); } } printf("\n"); } | /* solution by J. Packer */ void print_strings(char **s) { int i = 1; printf("%s", *(s + i++)); while(*(s + i) != NULL) printf(", %s", *(s + i++)); printf("\n"); } |
void print_strings(char *s[]) { int i = 1; while (s[i]) { printf("%s", s[i]); if (s[++i]) printf(", "); else printf("\n"); } } |
This same function takes a simple form in Java, since an array knows how long it is, so there is no need for a NULL at the end.
public class Quiz4 { public static void main(String[] args) { String[] m = {"none","Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec"}; String[] w = {"none","Mon","Tue","Wed","Thu","Fri","Sat","Sun"}; printString(m); printString(w); } public static void printString(String[] a) { for (int i = 1; i < a.length; i++) { System.out.print(a[i]); if (i == a.length - 1) System.out.println(); else System.out.print(", "); } } } |
Consider the following 2-dimensional array:
int a[4][6];
The array element at row 2 and column 4 can be referenced by the notation a[2][4]. Give another way to reference this array element that uses no square brackets. (This is the normal notation for an array of pointers to arrays, but the notation works here also.)