#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINES 25
void swapsp();
void sort(char *p[], int n);
void print_strings(char *p[], int n);
main()
{
int numwords = 30;
char *words[30]={ "computer", "hard", "soft", "clear", "message",
"button", "compact", "laser", "mouse", "speaker",
"error", "disk", "simple", "complex", "inside",
"watch", "bay", "already", "pixel", "nano",
"binary", "hex", "decimal", "java", "light",
"mega", "giga", "water", "caffiene", "camel"};
sort(words, numwords);
print_strings(words, numwords);
system ("PAUSE");
return(0);
}
void sort(char *p[], int n)
{
int a, b;
char *x;
for (a = 1; a < n; a++)
{
for (b = 0; b < n-1; b++)
{
swapsp();
}
}
}
void print_strings(char *p[], int n)
{
int counter;
for (counter = 0; counter < n; counter++)
printf("%s\n", p[counter]);
}
void swapsp()
{
if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings
{
x = parray;
p = p[b+1];
p[b+1] = x;
}
}
Thanks in advance..
errors I can't fix..
I have the following code..
It's supposed to sort the strings in the array in order..
I wrote the program, and it worked well.. After I corrected somethings.. other errors showed up..
What am I doing wrong?
It's been only fifteen minutes and you didn't bother to say what error you were getting. I think you have an unreasonable expectation for turn around time here.
void swapsp(){ if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings { x = parray; p = p[b+1]; p[b+1] = x; }}
i dont see where any of these variables are declared/defined. you probably want them as arguments to your function...
i fail to see how this code worked before, unless you deleted some stuff...
Sorry..
We're all programmers, and we all forget..
Here're the errors that I got..
I tried many different things, but it just doesn't work...
53 e:\cprogh~1\hw1\1.3\webspme.cpp
`p' undeclared (first use this function)
53 e:\cprogh~1\hw1\1.3\webspme.cpp
`b' undeclared (first use this function)
55 e:\cprogh~1\hw1\1.3\webspme.cpp
`x' undeclared (first use this function)
We're all programmers, and we all forget..
Here're the errors that I got..
I tried many different things, but it just doesn't work...
53 e:\cprogh~1\hw1\1.3\webspme.cpp
`p' undeclared (first use this function)
53 e:\cprogh~1\hw1\1.3\webspme.cpp
`b' undeclared (first use this function)
55 e:\cprogh~1\hw1\1.3\webspme.cpp
`x' undeclared (first use this function)
You need to pass in the arguments to your function swapsp()
Daed.
void sort(char *p[], int n){ int a, b; char *x; for (a = 1; a < n; a++) { for (b = 0; b < n-1; b++) { swapsp(); // Your swapsp funtion uses p and b, which are // currently unreachable. // Why not just use the strcmp, in this for() ? } }}
Daed.
Damn!!
it's even messier now.. I think I should take a break and look at it later.. this happens a lot..
it's even messier now.. I think I should take a break and look at it later.. this happens a lot..
for (b = 0; b < n-1; b++) { swapsp(p, b, x); } }}void print_strings(char *p[], int n){ int counter; for (counter = 0; counter < n; counter++) printf("%s\n", p[counter]);}void swapsp(int p, int b, int x){ if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings { x = p; p = p[b+1]; p[b+1] = x; }}
At a glance, I see that you need a return type on your main function:
int main()
{
...
}
I've never seen/used system("PAUSE"), so unless this is defined somewhere else, I think this is probably an error.
The variable b needs to be an input parameter for swapsp. This variable is local to sort, but swapsp does not know about it. Same goes for char* x. parray is not defined; I think you intended to use "p".
You also need to define char* x in the swapsp() function. For now, you should just merge swapsp into the sort().
Once this is up and running, you are going to get a runtime error, because you are accessing an array out of bounds. At the end of the inner loop, b will have the value of 29, and b+1 will equal 30.
int main()
{
...
}
I've never seen/used system("PAUSE"), so unless this is defined somewhere else, I think this is probably an error.
The variable b needs to be an input parameter for swapsp. This variable is local to sort, but swapsp does not know about it. Same goes for char* x. parray is not defined; I think you intended to use "p".
You also need to define char* x in the swapsp() function. For now, you should just merge swapsp into the sort().
[SOURCE]void sort(char *p[], int n){ int a, b; char *x; for (a = 1; a < n; a++) { for (b = 0; b < n-1; b++) { if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings { x = p; p = p[b+1]; p[b+1] = x; } } }}[/SOURCE]
Once this is up and running, you are going to get a runtime error, because you are accessing an array out of bounds. At the end of the inner loop, b will have the value of 29, and b+1 will equal 30.
Quote:Original post by nectron101
Damn!!
it's even messier now.. I think I should take a break and look at it later.. this happens a lot..for (b = 0; b < n-1; b++) { swapsp(p, b, x); } }}void print_strings(char *p[], int n){ int counter; for (counter = 0; counter < n; counter++) printf("%s\n", p[counter]);}void swapsp(int p, int b, int x){ if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings { x = p; p = p[b+1]; p[b+1] = x; }}
p is probably going to be char **
and x is a temporary char *, not a argument
Ok..
Here's what I have now, but there are still errors..
39 e:\cprogh~1\hw1\1.3\webspme.cpp
parse error before `]'
54 e:\cprogh~1\hw1\1.3\webspme.cpp
passing `char' to argument 1 of `strcmp(const char *, const char *)' lacks a cast
54 e:\cprogh~1\hw1\1.3\webspme.cpp
passing `char' to argument 2 of `strcmp(const char *, const char *)' lacks a cast
Here's what I have now, but there are still errors..
39 e:\cprogh~1\hw1\1.3\webspme.cpp
parse error before `]'
54 e:\cprogh~1\hw1\1.3\webspme.cpp
passing `char' to argument 1 of `strcmp(const char *, const char *)' lacks a cast
54 e:\cprogh~1\hw1\1.3\webspme.cpp
passing `char' to argument 2 of `strcmp(const char *, const char *)' lacks a cast
#include <stdlib.h>#include <stdio.h>#include <string.h>#define MAXLINES 25void swapsp();void sort(char *p[], int n);void print_strings(char *p[], int n);main(){ int numwords = 30; char *words[30]={ "computer", "hard", "soft", "clear", "message", "button", "compact", "laser", "mouse", "speaker", "error", "disk", "simple", "complex", "inside", "watch", "bay", "already", "pixel", "nano", "binary", "hex", "decimal", "java", "light", "mega", "giga", "water", "caffiene", "camel"}; sort(words, numwords); print_strings(words, numwords); system ("PAUSE"); return(0);}void sort(char *p[], int n){ int a, b; char *x; for (a = 1; a < n; a++) { for (b = 0; b < n-1; b++) { swapsp(p[], b); } }}void print_strings(char *p[], int n){ int counter; for (counter = 0; counter < n; counter++) printf("%s\n", p[counter]);}void swapsp(char p[], int b){ int x; if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings { x = p; p = p[b+1]; p[b+1] = x; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement