Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
(1).defines a pointer array as follows:
C
char *name[] ={"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"};


where name is an array of pointers pointing to strings.

(2).sorts the month names in alphabetic order by arranging the values in the name array so that
name[0] points to “April ”, name[1] points to “August ”, and so on.

(3).prints the month names in alphabetic order, one name per line.

For step (2), you can use any sorting algorithm (such as the selection sort, bubble sort or quick sort
algorithm) to sort the name array. You can use the standard C library function strcmp() to
compare two strings. The function is declared in the system header file string.h as follows:
int strcmp(const char *s1, const char *s2);

It compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if
s1 is found, respectively, to be less than, to match, or be greater than s2.



i am really bad when it comes to programming so any help would be appreciated..thanks a lot:)
Posted
Updated 14-Apr-10 1:31am
v3

This is a homework assignment. Just to be clear, we're not going to write your code for you.

What part of this are you hung up on?
 
Share this answer
 
bowww wrote:
i am really bad when it comes to programming so any help would be appreciated..thanks a lot

What about a career change?
:)
 
Share this answer
 
Since you can use any sorting algorithm, compare the strings in your head and provide coded output for the results. Problem solved.
 
Share this answer
 
ok i dont know how to do the sorting in part 2.
 
Share this answer
 
Try it :) :

C++
void SortNames(char** ppchNames, int iCount)
{
  for (int j = 0; j < iCount -1; j++) {
    for (int i = 0; i < iCount -1; i++) {
      if (0 < strcmp(ppchNames[i], ppchNames[i +1])) {
        char* pchTemp = ppchNames[i];
        ppchNames[i] = ppchNames[i +1];
        ppchNames[i +1] = pchTemp;
      }
    }
  }
}
 
void TheUsage() {
  char* names[] = {
    "bb",
    "gg",
    "hh",
    "aa"
  };
  SortNames(names, _countof(names));
}
 
Share this answer
 
Please try and validate it :) :

#include <iostream>
 
void SortNames(char** ppchNames, int iCount)
{
  for (int j = 0; j < iCount -1; j++) {
    for (int i = 0; i < iCount -1; i++) {
      if (0 < strcmp(ppchNames[i], ppchNames[i +1])) {
        char* pchTemp = ppchNames[i];
        ppchNames[i] = ppchNames[i +1];
        ppchNames[i +1] = pchTemp;
      }
    }
  }
}
 
void PrintNames(char** ppchNames, int iCount)
{
  for (int i = 0; i < iCount; i++) {
    cout << ppchNames[i] << "\n";
  }
}
 
int main()
{
  char* name[] = {
                  "January",
                  "February",
                  "March",
                  "April",
                  "May",
                  "June",
                  "July",
                  "August",
                  "September",
                  "October",
                  "November",
                  "December"
                 };
 
  cout << "-- before:" << "\n";
 
  PrintNames(name, _countof(name));
 
  SortNames(name, _countof(name));
 
  cout << "-- after:" << "\n";
 
  PrintNames(name, _countof(name));
 
  return 0;
}
 
Share this answer
 
v2
Yes, it must be validated by you :)

Try to use #include <stdio.h> instead of #include <iostream>

and replace cout << with functions like printf(..)... :)
 
Share this answer
 
C#
#include<stdio.h>
#include<math.h>
int i;
int j;
int iCount=0;
void SortNames(char** ppchNames, int iCount)
{
  for (int j = 0; j < iCount -1; j++) {
    for (int i = 0; i < iCount -1; i++) {
      if (0 < strcmp(ppchNames[i], ppchNames[i +1])) {
        char* pchTemp = ppchNames[i];
        ppchNames[i] = ppchNames[i +1];
        ppchNames[i +1] = pchTemp;
      }
    }
  }
}
void PrintNames(char** ppchNames, int iCount)
{
  for (int i = 0; i i < iCount; i++) {
   printf("%s\n", i);
  }
}
int main()
{
  char* name[] = {
                  "January",
                  "February",
                  "March",
                  "April",
                  "May",
                  "June",
                  "July",
                  "August",
                  "September",
                  "October",
                  "November",
                  "December"
                 };
    printf("%d\n", ppchNames);
  PrintNames(name, _countof(name));
  SortNames(name, _countof(name));
    printf("%d\n", ppchNames);

  PrintNames(name, _countof(name));
  return 0;
}



these are the errors:



VB
q11.c: In function ‘SortNames’:
q11.c:10: error: ‘for’ loop initial declaration used outside C99 mode
q11.c:11: error: ‘for’ loop initial declaration used outside C99 mode
q11.c: In function ‘PrintNames’:
q11.c:23: error: ‘for’ loop initial declaration used outside C99 mode
q11.c:23: error: syntax error before ‘i’
q11.c:23: error: syntax error before ‘)’ token
q11.c: In function ‘main’:
q11.c:31: error: stray ‘\226’ in program
q11.c:31: error: stray ‘\128’ in program
q11.c:31: error: stray ‘\156’ in program
q11.c:31: error: stray ‘\226’ in program
q11.c:31: error: stray ‘\128’ in program
q11.c:31: error: stray ‘\157’ in program
q11.c:31: error: ‘January’ undeclared (first use in this function)
q11.c:31: error: (Each undeclared identifier is reported only once
q11.c:31: error: for each function it appears in.)
q11.c:32: error: stray ‘\226’ in program
q11.c:32: error: stray ‘\128’ in program
q11.c:32: error: stray ‘\156’ in program
q11.c:32: error: stray ‘\226’ in program
q11.c:32: error: stray ‘\128’ in program
q11.c:32: error: stray ‘\157’ in program
q11.c:32: error: ‘February’ undeclared (first use in this function)
q11.c:33: error: stray ‘\226’ in program
q11.c:33: error: stray ‘\128’ in program
q11.c:33: error: stray ‘\156’ in program
q11.c:33: error: stray ‘\226’ in program
q11.c:33: error: stray ‘\128’ in program
q11.c:33: error: stray ‘\157’ in program
q11.c:33: error: ‘March’ undeclared (first use in this function)
q11.c:34: error: stray ‘\226’ in program
q11.c:34: error: stray ‘\128’ in program
q11.c:34: error: stray ‘\156’ in program
q11.c:34: error: stray ‘\226’ in program
q11.c:34: error: stray ‘\128’ in program
q11.c:34: error: stray ‘\157’ in program
q11.c:34: error: ‘April’ undeclared (first use in this function)
q11.c:35: error: stray ‘\226’ in program
q11.c:35: error: stray ‘\128’ in program
q11.c:35: error: stray ‘\156’ in program
q11.c:35: error: stray ‘\226’ in program
q11.c:35: error: stray ‘\128’ in program
q11.c:35: error: stray ‘\157’ in program
q11.c:35: error: ‘May’ undeclared (first use in this function)
q11.c:36: error: stray ‘\226’ in program
q11.c:36: error: stray ‘\128’ in program
q11.c:36: error: stray ‘\156’ in program
q11.c:36: error: stray ‘\226’ in program
q11.c:36: error: stray ‘\128’ in program
q11.c:36: error: stray ‘\157’ in program
q11.c:36: error: ‘June’ undeclared (first use in this function)
q11.c:37: error: stray ‘\226’ in program
q11.c:37: error: stray ‘\128’ in program
q11.c:37: error: stray ‘\156’ in program
q11.c:37: error: stray ‘\226’ in program
q11.c:37: error: stray ‘\128’ in program
q11.c:37: error: stray ‘\157’ in program
q11.c:37: error: ‘July’ undeclared (first use in this function)
q11.c:38: error: stray ‘\226’ in program
q11.c:38: error: stray ‘\128’ in program
q11.c:38: error: stray ‘\156’ in program
q11.c:38: error: stray ‘\226’ in program
q11.c:38: error: stray ‘\128’ in program
q11.c:38: error: stray ‘\157’ in program
q11.c:38: error: ‘August’ undeclared (first use in this function)
q11.c:39: error: stray ‘\226’ in program
q11.c:39: error: stray ‘\128’ in program
q11.c:39: error: stray ‘\156’ in program
q11.c:39: error: stray ‘\226’ in program
q11.c:39: error: stray ‘\128’ in program
q11.c:39: error: stray ‘\157’ in program
q11.c:39: error: ‘September’ undeclared (first use in this function)
q11.c:40: error: stray ‘\226’ in program
q11.c:40: error: stray ‘\128’ in program
q11.c:40: error: stray ‘\156’ in program
q11.c:40: error: stray ‘\226’ in program
q11.c:40: error: stray ‘\128’ in program
q11.c:40: error: stray ‘\157’ in program
q11.c:40: error: ‘October’ undeclared (first use in this function)
q11.c:41: error: stray ‘\226’ in program
q11.c:41: error: stray ‘\128’ in program
q11.c:41: error: stray ‘\156’ in program
q11.c:41: error: stray ‘\226’ in program
q11.c:41: error: stray ‘\128’ in program
q11.c:41: error: stray ‘\157’ in program
q11.c:41: error: ‘November’ undeclared (first use in this function)
q11.c:42: error: stray ‘\226’ in program
q11.c:42: error: stray ‘\128’ in program
q11.c:42: error: stray ‘\156’ in program
q11.c:42: error: stray ‘\226’ in program
q11.c:42: error: stray ‘\128’ in program
q11.c:42: error: stray ‘\157’ in program
q11.c:43: error: ‘December’ undeclared (first use in this function)
q11.c:44: error: ‘ppchNames’ undeclared (first use in this function)
 
Share this answer
 
And now ? :)

#include <stdio.h>
  
void SortNames(char** ppchNames, int iCount)
{
  int i,j;
  for (j = 0; j < iCount -1; j++) {
    for (i = 0; i < iCount -1; i++) {
      if (0 < strcmp(ppchNames[i], ppchNames[i +1])) {
        char* pchTemp = ppchNames[i];
        ppchNames[i] = ppchNames[i +1];
        ppchNames[i +1] = pchTemp;
      }
    }
  }
}
  
void PrintNames(char** ppchNames, int iCount)
{
  int i;
  for (i = 0; i < iCount; i++) {
    printf("%s\n", ppchNames[i]);
  }
}
  
int main()
{
  char* name[] = {
                  "January",
                  "February",
                  "March",
                  "April",
                  "May",
                  "June",
                  "July",
                  "August",
                  "September",
                  "October",
                  "November",
                  "December"
                 };
 
  printf("-- before:\n");
 
  PrintNames(name, _countof(name));
 
  SortNames(name, _countof(name));
 
  printf("-- after:\n");
 
  PrintNames(name, _countof(name));
 
  return 0;
}
 
Share this answer
 
v3
it still gives me:


VB
q11.c: In function ‘SortNames’:
q11.c:8: error: ‘lt’ undeclared (first use in this function)
q11.c:8: error: (Each undeclared identifier is reported only once
q11.c:8: error: for each function it appears in.)
q11.c:8: error: syntax error before ‘;’ token
q11.c:8: error: syntax error before ‘)’ token
q11.c:11: error: ‘pchTemp’ undeclared (first use in this function)
q11.c: At top level:
q11.c:15: error: syntax error before ‘}’ token
q11.c: In function ‘main’:
q11.c:28: error: stray ‘\226’ in program
q11.c:28: error: stray ‘\128’ in program
q11.c:28: error: stray ‘\156’ in program
q11.c:28: error: stray ‘\226’ in program
q11.c:28: error: stray ‘\128’ in program
q11.c:28: error: stray ‘\157’ in program
q11.c:28: error: ‘January’ undeclared (first use in this function)
q11.c:29: error: stray ‘\226’ in program
q11.c:29: error: stray ‘\128’ in program
q11.c:29: error: stray ‘\156’ in program
q11.c:29: error: stray ‘\226’ in program
q11.c:29: error: stray ‘\128’ in program
q11.c:29: error: stray ‘\157’ in program
q11.c:29: error: ‘February’ undeclared (first use in this function)
q11.c:30: error: stray ‘\226’ in program
q11.c:30: error: stray ‘\128’ in program
q11.c:30: error: stray ‘\156’ in program
q11.c:30: error: stray ‘\226’ in program
q11.c:30: error: stray ‘\128’ in program
q11.c:30: error: stray ‘\157’ in program
q11.c:30: error: ‘March’ undeclared (first use in this function)
q11.c:31: error: stray ‘\226’ in program
q11.c:31: error: stray ‘\128’ in program
q11.c:31: error: stray ‘\156’ in program
q11.c:31: error: stray ‘\226’ in program
q11.c:31: error: stray ‘\128’ in program
q11.c:31: error: stray ‘\157’ in program
q11.c:31: error: ‘April’ undeclared (first use in this function)
q11.c:32: error: stray ‘\226’ in program
q11.c:32: error: stray ‘\128’ in program
q11.c:32: error: stray ‘\156’ in program
q11.c:32: error: stray ‘\226’ in program
q11.c:32: error: stray ‘\128’ in program
q11.c:32: error: stray ‘\157’ in program
q11.c:32: error: ‘May’ undeclared (first use in this function)
q11.c:33: error: stray ‘\226’ in program
q11.c:33: error: stray ‘\128’ in program
q11.c:33: error: stray ‘\156’ in program
q11.c:33: error: stray ‘\226’ in program
q11.c:33: error: stray ‘\128’ in program
q11.c:33: error: stray ‘\157’ in program
q11.c:33: error: ‘June’ undeclared (first use in this function)
q11.c:34: error: stray ‘\226’ in program
q11.c:34: error: stray ‘\128’ in program
q11.c:34: error: stray ‘\156’ in program
q11.c:34: error: stray ‘\226’ in program
q11.c:34: error: stray ‘\128’ in program
q11.c:34: error: stray ‘\157’ in program
q11.c:34: error: ‘July’ undeclared (first use in this function)
q11.c:35: error: stray ‘\226’ in program
q11.c:35: error: stray ‘\128’ in program
q11.c:35: error: stray ‘\156’ in program
q11.c:35: error: stray ‘\226’ in program
q11.c:35: error: stray ‘\128’ in program
q11.c:35: error: stray ‘\157’ in program
q11.c:35: error: ‘August’ undeclared (first use in this function)
q11.c:36: error: stray ‘\226’ in program
q11.c:36: error: stray ‘\128’ in program
q11.c:36: error: stray ‘\156’ in program
q11.c:36: error: stray ‘\226’ in program
q11.c:36: error: stray ‘\128’ in program
q11.c:36: error: stray ‘\157’ in program
q11.c:36: error: ‘September’ undeclared (first use in this function)
q11.c:37: error: stray ‘\226’ in program
q11.c:37: error: stray ‘\128’ in program
q11.c:37: error: stray ‘\156’ in program
q11.c:37: error: stray ‘\226’ in program
q11.c:37: error: stray ‘\128’ in program
q11.c:37: error: stray ‘\157’ in program
q11.c:37: error: ‘October’ undeclared (first use in this function)
q11.c:38: error: stray ‘\226’ in program
q11.c:38: error: stray ‘\128’ in program
q11.c:38: error: stray ‘\156’ in program
q11.c:38: error: stray ‘\226’ in program
q11.c:38: error: stray ‘\128’ in program
q11.c:38: error: stray ‘\157’ in program
q11.c:38: error: ‘November’ undeclared (first use in this function)
q11.c:39: error: stray ‘\226’ in program
q11.c:39: error: stray ‘\128’ in program
q11.c:39: error: stray ‘\156’ in program
q11.c:39: error: stray ‘\226’ in program
q11.c:39: error: stray ‘\128’ in program
q11.c:39: error: stray ‘\157’ in program
q11.c:40: error: ‘December’ undeclared (first use in this function)
 
Share this answer
 
Please copy the improved code above and try again :)
 
Share this answer
 
i did and it still gives this bunch of errors :(
 
Share this answer
 
oh now it gives me this:


Undefined symbols:
"__countof", referenced from:
_main in ccbdcvAC.o
_main in ccbdcvAC.o
_main in ccbdcvAC.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
 
Share this answer
 
Try to replace _countof(name) with sizeof(name)/sizeof(char*) :)
 
Share this answer
 
ya i think it works now:) except that i should've used:

int strcmp(const char *s1, const char *s2);

It compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if
s1 is found, respectively, to be less than, to match, or be greater than s2.



thanks a lot for your efforts and trying to help. it is really sweet of u :)
 
Share this answer
 
The function strcmp(..) is already used,
see at the if line inside of SortNames(..) :)
 
Share this answer
 
ya right found it :)
thanks a lot.
are you a programmer? i need help with my assignment if you can pleaseee.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900