- 38,138
- 10,725
Yes, exactly.
Oh, good. I can still write C. Pascal is the language I know best.Mark44 said:Yes, exactly.
I read everything you write and try to apply it. And I really appreciate you spending the time to help me with this. What did you see that I didn't do that you mentioned previously? I thought I changed them all.Please go back and carefully reread posts 17, 20, and 21. I spent a lot of time writing them, and you are ignoring (or not understanding) some of what I wrote. I don't want to have to write it again.
If there is something I said before that you don't understand - ask.
I forgot to take it out of case 1. I took it out of the fillArray function, but I left it in case 1 accidentally.When you call fillArray in case 1, you have
fillArray(int ary[], int range, int getOption);
You should NOT have the types in the parameter list.
Also, why is getOption listed here? Your program has already set the value of option.
getOption should have been a function, not a variable, and it definitely should not be a parameter to fillArray.
#include <stdbool.h>
#include <stdio.h>
int fillArray (int ary[], int range);
void printArray ();
int binarySearch (int list[], int end, int target, int *location);
int seqSearch (int list[], int last, int target, int *location);
void sortArray (int list[], int last);
int main()
{
bool sorted;
int option;
int getOption;
int range;
int i;
int ary[50];
int list[50];
int last;
int target;
int location;
int end;
while (option != 5)
{
printf("1- Fill Array\n2- Print Array\n3- Search Array\n4- Sort Array");
scanf("%l", &option);
switch (option)
{
case 1:
fillArray(ary[50], range);
sorted = false;
break;
case 2:
printArray();
break;
case 3:
if (sorted)
binarySearch(list[50], end, target, *location);
else
seqSearch(list[50], last, target, *location);
break;
case 4:
sortArray(list[50], last);
sorted = true;
break;
default:
printf("%c is an invalid choice.\n");
}
return 0;
}
int fillArray (int ary[], int range)
{
int range;
srand(time(NULL));
range = (999 - 0);
}
void printArray ()
{
for(i=0; i<50; i++);
printf("%d", ary[i]);
return 0;
}
int binarySearch (int list, int end, int target, int* location)
{
int first;
int mid;
int last[50];
first = 0;
last = end;
while (first <= last)
{
mid = (first + last) / 2;
if (target > list[mid])
first = mid + 1;
else if (target < list[mid])
last = mid - 1;
else
first = last + 1;
}
*location = mid;
return target == list[mid];
}
int seqSearch (int list[], int last, int target, int* location)
{
int looker;
int found;
looker = 0;
while (looker < last && target != list[looker])
looker++;
*location = looker;
found = (target == list[looker]);
return found;
}
void sortArray (int list[], int last)
{
int temp;
int walker;
for(int current = 0; current < last; current++)
{
for (int walker = last; walker > current; walker--)
if (list[walker] < list[walker - 1])
temp = list[walker];
list[walker] = list[walker - 1];
list[walker - 1] = temp;
}
return 0;
}
leroyjenkens said:It says "invalid argument type of 'unary *' (have 'int')" on the second line below case 3.
I don't even know what that means.
Same problem in your call to seqSearch as described above.leroyjenkens said:It says the same thing 2 lines below that.
Here's your code for fillArray.leroyjenkens said:It says "'range' redeclared as a different kind of symbol."
I don't know why that's a problem, but I don't know how to fix it.
int fillArray (int ary[], int range)
{
int range;
srand(time(NULL));
range = (999 - 0);
}
What difference do you notice between these two lines of code? These are the function headers for your two search routines. One of them is giving you and error and the other is not.leroyjenkens said:And in the code I got from the book, it has list[mid] in the binary search function. The compiler is saying that's neither an array nor a pointer. What is list[mid] and why is it in the book if it's invalid?
int binarySearch (int list, int end, int target, int* location)
{
...
}
int seqSearch (int list[], int last, int target, int* location)
{
...
}
What do I initialize them as? Like for example, I have int option; I thought the function itself gave them values. Do I need to give them all values?location is an int value, so you cannot dereference it as if it were a pointer.
Also, every one of the variables you declare at the top of main is uninitialized, so when you pass them to your functions, it's GIGO (garbage in, garbage out).
Whenever you pass a value to a function, you need to make sure that you know its value going in. The only exceptions are when the parameter is of some pointer type, which would include array types.
If it's 5, the program is supposed to quit. 5 would make the loop stop, right? Is that not a good way to do it?In your while loop, the first thing you test is that option != 5. How do you know that option doesn't have the value 5?
I took it out of the body. Do I need to put it anywhere else, or is it being declared in the function argument?In the parameter list, you're telling the compiler that range is an int. Inside the body of the function, you are redeclaring range. You can't do that.
Sorry about that, I left that the same because I was trying to find the example in my book that gave me the exact syntax I need and I couldn't find it.Also, and as mentioned now three times, this function is broken! You are setting range to 999. One of the many things you've missed that I've said before is that what you're calling the range needs to be two separate variables, a min. value and a max. value.
Ok I changed that and it's accepted now.What difference do you notice between these two lines of code? These are the function headers for your two search routines. One of them is giving you and error and the other is not.
Mark44 said:location is an int value, so you cannot dereference it as if it were a pointer.
Also, every one of the variables you declare at the top of main is uninitialized, so when you pass them to your functions, it's GIGO (garbage in, garbage out).
Whenever you pass a value to a function, you need to make sure that you know its value going in. The only exceptions are when the parameter is of some pointer type, which would include array types.
What function? The getOption function was supposed to return a value to the option variable, but you decided not to write this function. Since you don't have a function that sets this variable you need to set it yourself, like, say, to -1.leroyjenkins said:What do I initialize them as? Like for example, I have int option; I thought the function itself gave them values. Do I need to give them all values?
Mark44 said:In your while loop, the first thing you test is that option != 5. How do you know that option doesn't have the value 5?
An uninitialzed variable could have any value in it, including 5. If that happened, your loop wouldn't run at all.leroyjenkins said:If it's 5, the program is supposed to quit. 5 would make the loop stop, right? Is that not a good way to do it?
Mark44 said:In the parameter list, you're telling the compiler that range is an int. Inside the body of the function, you are redeclaring range. You can't do that.
leroyjenkins said:I took it out of the body. Do I need to put it anywhere else, or is it being declared in the function argument?
Mark44 said:Also, and as mentioned now three times, this function is broken! You are setting range to 999. One of the many things you've missed that I've said before is that what you're calling the range needs to be two separate variables, a min. value and a max. value.
You need to decide what fillArray should do. Part of that planning is figuring out what inputs it needs (the arguments) and what, if any, the outputs should be. If you want to specify a range of values for the elements of the array, you need to pass in two values.leroyjenkins said:Sorry about that, I left that the same because I was trying to find the example in my book that gave me the exact syntax I need and I couldn't find it.
Does it just need to be like int minRange; and int maxRange; or something like that?
Mark44 said:What difference do you notice between these two lines of code? These are the function headers for your two search routines. One of them is giving you and error and the other is not.
How did you change it? It could be that you have now broken both functions.leroyjenkins said:Ok I changed that and it's accepted now.
It means you are trying to do division with two types that aren't compatible.leroyjenkins said:I still have it telling me error: invalid operands to binary / (have 'int *' and 'int')
in the binarySearch function, 2 lines under "while". What does that mean?
int binarySearch (int list, int end, int target, int* location)
{
int first;
int mid;
int last[50];[/color]
first = 0;
last = end;
while (first <= last)
{
mid = (first + last) / 2;[/color]
...
}
I thought the functions used the value the user inputted.What function? The getOption function was supposed to return a value to the option variable, but you decided not to write this function. Since you don't have a function that sets this variable you need to set it yourself, like, say, to -1.
Oh, so that's why I'd set it to -1 so it doesn't get randomly set?An uninitialzed variable could have any value in it, including 5. If that happened, your loop wouldn't run at all.
fillArray should get random numbers into an array between "1 and 999 inclusive", which is what the assignment states.You need to decide what fillArray should do.
Like this? range = (1, 999);Part of that planning is figuring out what inputs it needs (the arguments) and what, if any, the outputs should be. If you want to specify a range of values for the elements of the array, you need to pass in two values.
The header line of a function is a kind of contract between the function and whoever calls it (main in your case).
lol that's a good possibility. I just changed the one that wasn't giving me an error.How did you change it? It could be that you have now broken both functions.
int binarySearch (int list[], int end, int target, int* location)
Oh ok I fixed that.It means you are trying to do division with two types that aren't compatible.
Not sure why I changed that. I think it's because I was getting an error from list[mid] and for some reason putting [50] made that error go away. I took it away again and it's not giving me an error. I don't know. It's just list now.This code was OK before, but you changed it, so now you're getting the error you showed. In a previous iteration, last was an int variable. Now it's an array of ints. Why?
#include <stdbool.h>
#include <stdio.h>
int fillArray (int range);
void printArray ();
int binarySearch (int list[], int end, int target, int *location);
int seqSearch (int list[], int last, int target, int *location);
void sortArray (int list[], int last);
int main()
{
bool sorted;
int option = -1;
int getOption;
int range;
int i;
int ary[50];
int list[50];
int last;
int target;
int *location;
int end;
while (option != 5)
{
printf("1- Fill Array\n2- Print Array\n3- Search Array\n4- Sort Array");
scanf("%l", &option);
switch (option)
{
case 1:
fillArray(range);
sorted = false;
break;
case 2:
printArray();
break;
case 3:
if (sorted)
binarySearch(list[50], end, target, *location);
else
seqSearch(list[50], last, target, *location);
break;
case 4:
sortArray(list[50], last);
sorted = true;
break;
default:
printf("%c is an invalid choice.\n");
}
}
return 0;
}
int fillArray (int range)
{
srand(time(NULL));
range = (1, 999);
}
void printArray ()
{
for(i=0; i<50; i++);
printf("%d", ary[i]);
return 0;
}
int binarySearch (int list[], int end, int target, int* location)
{
int first;
int mid;
int last;
first = 0;
last = end;
while (first <= last)
{
mid = (first + last) / 2;
if (target > list[mid])
first = mid + 1;
else if (target < list[mid])
last = mid - 1;
else
first = last + 1;
}
*location = mid;
return target == list[mid];
}
int seqSearch (int list[], int last, int target, int* location)
{
int looker;
int found;
looker = 0;
while (looker < last && target != list[looker])
looker++;
*location = looker;
found = (target == list[looker]);
return found;
}
void sortArray (int list[], int last)
{
int temp;
int walker;
for(int current = 0; current < last; current++)
{
for (int walker = last; walker > current; walker--)
if (list[walker] < list[walker - 1])
temp = list[walker];
list[walker] = list[walker - 1];
list[walker - 1] = temp;
}
return;
}
The semester is over now, and we were given an extra day to turn in this last assignment. This menu one is the only program left in the assignment that I haven't finished. I have to drive up to school to turn in the hard copy before 5 and I have until midnight to email the instructor the actual programs.Leroy,
You mentioned before in this or another thread that there are four programming assignments in the class you're taking. I have to say that that doesn't sound like the best way to learn programming. A better approach, I believe, is to write more programs, with each one being focussed on a smaller number of programming concepts.
It was a 6 week summer class. I was there 4 days a week. I probably would've done a lot better had it been a fall or spring class. Few people in the class did do well, from what I heard in the computer lab, so that made me feel a bit better.After you're comfortable with creating algorithms, parameter passing, and pointers, then it's reasonable to combine these ideas in larger programs such as the one you're working on.
The class you're taking sounds like an accelerated programming class. That would probably be OK for someone with some experience in another programming language, but for someone like you with no prior experience, it doesn't add up to a good learning experience.
A function can get input from the user (via scanf or other standard library input function) OR it can use the inputs passed in the call to its parameters.leroyjenkens said:I thought the functions used the value the user inputted.
If the problem statement requires this function, you need it, otherwise not. If it's not required you can get away with what you have.leroyjenkens said:Do I need the getOption function or will it work the way I have it now?
Right.leroyjenkens said:Oh, so that's why I'd set it to -1 so it doesn't get randomly set?
I think you're misreading what it says in the book. 20 - 10 isn't two numbers; it's one number, 10. In the same way, 999 - 1 isn't two numbers; it's one number, 998.leroyjenkens said:fillArray should get random numbers into an array between "1 and 999 inclusive", which is what the assignment states.
I used an example from the book to get the random numbers, and I replaced their (20 - 10), which was random numbers between 20 and 10, with my own range, which is (999 - 1). I assumed just replacing the range of numbers needed would work. I guess not.
Like this? range = (1, 999);
No, that's fine. Keep in mind that the list parameter is not really an array, despite the "int list[]" notation. It's really the address of an int, so could also be written as "int * list".leroyjenkens said:lol that's a good possibility. I just changed the one that wasn't giving me an error.
So it looks like this now...
I guess it could still be wrong.Code:int binarySearch (int list[], int end, int target, int* location)
Actually, this is still broken, for two reasons.leroyjenkens said:Oh ok I fixed that.
mid = (first + last[50]) / 2;
leroyjenkens said:Not sure why I changed that. I think it's because I was getting an error from list[mid] and for some reason putting [50] made that error go away. I took it away again and it's not giving me an error. I don't know. It's just list now.
Here's what I have right now...
It's giving me an error on the very last line at the }. It says "expected declaration or statement at end of input". I've tried putting doing stuff with the return; but nothing is taking that error away.Code:#include <stdbool.h> #include <stdio.h> int fillArray (int range); void printArray (); int binarySearch (int list[], int end, int target, int *location); int seqSearch (int list[], int last, int target, int *location); void sortArray (int list[], int last); int main() { bool sorted; int option = -1; int getOption; int range; int i; int ary[50]; int list[50]; int last; int target; int *location; int end; while (option != 5) { printf("1- Fill Array\n2- Print Array\n3- Search Array\n4- Sort Array"); scanf("%l", &option); switch (option) { case 1: fillArray(range); sorted = false; break; case 2: printArray(); break; case 3: if (sorted) binarySearch(list[50], end, target, *location); else seqSearch(list[50], last, target, *location); break; case 4: sortArray(list[50], last); sorted = true; break; default: printf("%c is an invalid choice.\n"); } } return 0; } int fillArray (int range) { srand(time(NULL)); range = (1, 999); } void printArray () { for(i=0; i<50; i++); printf("%d", ary[i]); return 0; } int binarySearch (int list[], int end, int target, int* location) { int first; int mid; int last; first = 0; last = end; while (first <= last) { mid = (first + last) / 2; if (target > list[mid]) first = mid + 1; else if (target < list[mid]) last = mid - 1; else first = last + 1; } *location = mid; return target == list[mid]; } int seqSearch (int list[], int last, int target, int* location) { int looker; int found; looker = 0; while (looker < last && target != list[looker]) looker++; *location = looker; found = (target == list[looker]); return found; } void sortArray (int list[], int last) { int temp; int walker; for(int current = 0; current < last; current++) { for (int walker = last; walker > current; walker--) if (list[walker] < list[walker - 1]) temp = list[walker]; list[walker] = list[walker - 1]; list[walker - 1] = temp; } return; }
Edit: Ok I found the problem, I was missing a curly bracket up in the main. So at least there's no errors. My program runs. I wanted to get it running so I could actually see the problems it has. When I hit a number, it begins spamming the screen with the list menu.
Thanks.
Even if you manage to eke out a grade of C, which doesn't seem likely to me, I would still advise taking this class again. As I mentioned earlier, having a physics degree without any experience in programming would probably limit your options.leroyjenkens said:The semester is over now, and we were given an extra day to turn in this last assignment. This menu one is the only program left in the assignment that I haven't finished. I have to drive up to school to turn in the hard copy before 5 and I have until midnight to email the instructor the actual programs.
I'd never done any programming before this class, so I had no idea how to study. Apparently making programs is the best way to do that, because just reading the book and putting in a few codes from the examples doesn't help me. Now I know. This is just a prereq class for my physics degree, so I'm just trying to get a C. Not sure if I've gotten that C, though. My exam scores were high D's and I did decently on the programs and quizzes. I'm hoping to get this program working so that it'll be enough to get me a C. I really don't want to take this class over again.
It shouldn't make you feel all that good, because you're the one who is getting the grade.leroyjenkens said:It was a 6 week summer class. I was there 4 days a week. I probably would've done a lot better had it been a fall or spring class. Few people in the class did do well, from what I heard in the computer lab, so that made me feel a bit better.
I took something out of it. Here's the whole thing.I think you're misreading what it says in the book. 20 - 10 isn't two numbers; it's one number, 10. In the same way, 999 - 1 isn't two numbers; it's one number, 998.
You should completely get rid of range as a variable, and instead use two different variables, say minVal and maxVal.
The statement you show above, range = (1, 999); , sets range to 999. To understand why this happens, look up a description of the comma operator in C.
srand(time(NULL));
range = (999 - 1) + 1;
randNo = rand() % range + 1;
I can't express how much I hate this class. I've never hated a subject, so this is a first. If I pass this class, I'll never touch programming again, unless I lose this feeling of hatred I have for it. If that limits my options as a physics person, then so be it. I've got quite a few years before I'd be an actual physicist anyway.Even if you manage to eke out a grade of C, which doesn't seem likely to me, I would still advise taking this class again. As I mentioned earlier, having a physics degree without any experience in programming would probably limit your options.
It makes me feel good because this whole semester I felt like I have a mental handicap for how much I struggle with this. This is the first time I've felt like I'm incapable of learning something. It's not a good feeling. So to find out I'm not the only one makes me feel better, even if I do fail.It shouldn't make you feel all that good, because you're the one who is getting the grade.