Using fgets(), with the 30-1, there is NO chance of a buffer over-run. None!
You can press keys until you're blue in the face, and it won't accept any more char's.
Using fgets(), with the 30-1, there is NO chance of a buffer over-run. None!
You can press keys until you're blue in the face, and it won't accept any more char's.
I should have seen this - you are using the char *molid, etc., and they have never been given a valid address.
usually, that causes a seg fault.
You can't delete an element from an array - they are fixed.
Why don't you post a simple example of what you want to do. Your description is dodgy, (or maybe it's just too early?). ;)
He doesn't need to change the size of his array - just use the code like I posted. There are very few words longer than 30 char's, but he can make it more than 30 if he needs it.
it's not bad practice - you're not seeing what I'm suggesting, or you wouldn't be harping on this. Didn't you see "MaxLength" in my code I posted earlier?
C'mon - tick tock, hop on board, train she is a'moving.
Yeah, I saw your good suggestion on the sort problem, but I had already posted before I read through your post.
You know as well as I do that if you absolutely want to save the maximum amount of space, and still get every long word, you would use a "ragged" 2D pointer array. It's just a bit above his skill level atm, and may be more than he needs, anyway.
No. ;)
He can use your fgets() suggestion, to limit the number of char's that can be input. There is no need to use the "input" array, AFAIK.
He should definitely keep the 2D word array, and it is certainly NOT "bad" practice.
He has some other code that is useless, such as the loop with "count", that isn't needed. Whether the string contains spaces or not won't matter with fgets():
int numNames, MaxLength=30;
printf("\n How many names do you want to enter? [1-10] ");
scanf("%d", &numNames);
getchar(); //remove the leftover '\n'
for(i=0;i<numNames;i++)
fgets(word[i], MaxLength-1, stdin);
After that, sorting the strings is straight forward. I pointed out his mistake earlier in his sorting.
If he has any problems I'll be glad to sort out his sort, again. ;)
I don't know a single one, it's a diminishing language for studying. Google does have lots of hits, however. Several tutorials included.
First off ALWAYS put your code, between the code tags! It's VERY hard to study code like that is smashed together as html text.
Second, my atoi() function, is in stdlib.h, not ctype.h. Please recheck that on your compiler.
I can't tell if I'm looking at comma's or semi-colons on your code - post it up in between code tags. It will be ignored pretty much, otherwise.
Don't post about Shell sort and all the rest - I know Shell sort. Post up about your own problems with your own code. And please, limit the pic's - just copy and paste your code between code tags.
Well, casting is not the same thing as typecasting. Typecasting just creates an "alias" (another name), your program can use, for a certain datatype (typically a struct name).
A cast in C is redundant, generally. It doesn't hurt to make them (if you're very careful to make it right), but it can 1) hide an error (when you forget to include stdlib.h), and 2) gives you a chance of goofing it up.
On RARE occasions, you will need to cast the return from malloc(), but it IS very rare.
Auhors (as you will see when you have more experience), are not necessarily good programmer's, and have taken their code (with slight modifications), from others, or have not updated their programming skills since Christ was a corporal, or it's just an error, or whatever. Gods of programming, they are not.
Get a copy of "The C Programming Language" by Kernighan and Ritchie, (second or latest edition), and you'll have the bible of C programming, straight from the creators of the language.
The first thing to know about the 15 tiles puzzle program is that half of the random boards will be completely unsolvable. That was the gimmick when the puzzle was marketed in the 1940's and 50's. It was sold with the puzzle in an initial position that it could NOT be solved.
(That may be why your prof only wants 3 tiles out of place - so it can be solved, but I'm not sure.)
The smart ass even tried to patent that puzzle, that way, but the patent office said he couldn't patent it unless he could show it was solvable (they were onto him).
Lots of info on the net about the 15 tiles puzzle, including applets that let you play, etc. Lots on info on the A* search, some on ID as well. Haven't googled for Manhattan Distance, but it was mentioned in Donald Knuth's tile solving puzzle program. It's on his program's site. No idea what algo he used, however.
More on Google, I'm sure.
A few people have posted here about this puzzle, but they quickly wander off when they see that we don't have "the codez" for them to call their own. I have a 15 tile puzzle program, but it's VERY different, and has no search - quite explicit and fast.
If you need help with ID, the best place to go to would be the Open Chess forum, in the programming sub-forum. They use ID ALL …
Are you trying to USE atoi() in your program, or are you trying to make your own atoi() function?
The C atoi() function will need a pointer to the char array, not this:
atoi({abcdefghij});
I have no idea what the abcd... stuff is all about.
ddd ccc eee is not in sorted order, so I'm not sure if you want to sort in ascending or descending order.
You should keep the 2D array, no need to change that.
you can't use temp like you have it in the sort portion. When you assign it to the address for string1, that's fine.
But then you strcpy string2 into string1! Guess what temp is pointing to NOW?
Then you're putting string2 right back into string2 with the final assignment from temp to string2.
Print out your sort when it swaps, and see it. Put in a getchar() to stop it after every swap, if you don't step through it with watches set.
Better to make temp an array, instead of a ptr, imo.
No casting or typecasting is needed for malloc(). It returns a void pointer which the program will automatically change to the right type, for you. Very rarely, you'll need to do a cast of the pointer, manually.
You need to include stdlib.h for malloc to work.
If you need more help, catch some of the excellent tutorials on line from Google, or post up your code, so we can see WTF you're trying to do, and give specific advice.
Advice like this is never great, because it can't be nearly as thorough as a tutorial dedicated to the subject, and it can't be very specific, because there is no code.
You need:
1) To put code tags around all your code, for the forum. Otherwise your code is very hard to study. Highlight it, and click on the CODE icon in the editing window.
2) Show an example of the input - just two or thee lines is enough
3) Show an example of the output you want
It appears (of course), that you are making an error with strtok()
strtok(string1, string2);
(Look! Code tags!!) ;)
scans string1 for the first token matching string2. Why the NULL in yours?
Did you read the "Readme" file in the TC directory?
Especially the install instructions?
rand() will give you the SAME sequence of random numbers every time (it's used that way for testing programs and equipt.).
srand() will give rand() a new starting place or "seed"
/* resets the random number function,
based on system time */
time_t t;
srand(time(&t));
All these random generators like this, are really not so random ("pseudo random"), unless handled just right - so don't do any serious stuff and count on it being truly 100% random, OK?
If you have a number in base 10 (normal base), and use the mod operator on it, what do you get?
int number = 123;
printf("\n %d", (number % 10));
now divide the number by 10. You've now "peeled off" the right most digit from the number.
You can continue doing this in a loop, until the number disappears. Save the first and the last digit, and there you go.
If you need any more help with your program, post your code so we can see the details of what you're referring to. Doesn't have to be the whole program, but at least the part you're referring to.
AND do use code tags around any posted code - please!
rand() can give you a specific range of random numbers. Just set it up for 1 to 5, four times.
1
2
3
5
Inside a loop, you can put the "number" together, from those digits if you want:
5 * 10^0 +
3 * 10^1 +
2 * 10^2 +
etc. You see the pattern.
Post up your function, and we can be a lot more specific with the help.
You need to ask questions about your program, rather than posting your assignment and saying "thanks" for the solution.
It is your assignment, after all. You won't learn by not working with it.
And Welcome to the Forum, Ahmed! ;)
Just a quick note:
1) On programming forums, you NEED to use CODE tags. In the editing window, highlight your code, and click on the
icon.
Otherwise, your code is difficult to study, and will be generally ignored.
2) You don't have any #include files listed in your program.
3) You are using tokensize before you initialize it to anything.
4) You need to include <stdlib.h> for malloc(), and there is no need to cast the pointer it returns - it will be done for you, in C.
5) I would use a struct containing one name, and six integers. Declare the struct's members above main(). Inside main(), create your array of structs, and you can do all the checking with loops.
6) There is a simple way of doing this, with just one string array[][] and one number array[][]. That might be a lot easier that using structs and etc., as in #5.
I don't see any need to tokenize anything.
[code]
While((you have data)) { //use fscanf() or preferrably, fgets()
get the name
for(i=0;i<6;i++) {
get the six numbers
and handle them as you wish
}
}
/* you have all your data, now, proceed
to the next step of your program */
Here's one big error:
scanf("%s",inpt_str1);
for(i=0;inpt_str1[i]!='\n';i++)
{
len1+=1;
concat_str[i]=inpt_str1[i];
}
scanf() does NOT include the newline char, into the string - that's fgets(). scanf() (infamously!) leaves the newline behind.
So there is no stopping your loop. Change the '\n' to '\0' (the end of string marker) (which scanf() DOES include, for strings.
You do this same error in two places, at least.
The code I was referring to was for Tic-Tac-Toe. There were two problems:
1) One version used OOP style, and is in C++.
2) Tic-Tac-Toe #2 uses alpha-beta version of DFS, and I'm unable to locate the file. (It may have been lost in a HUGE virus attack I had awhile back).
The second link I posted, has both DFS and BFS code in C, although it's not set up for your 8 tile puzzle.
Either you want to start working on your program, or you don't. If you will start working on your own 8 tile puzzle program, I will be able to help you. So far, I don't even know if your "8 tile puzzle" is the SAME 8 tile puzzle (has the same movement rules), as the puzzle that I'm familiar with.
Can the 4 center tiles in your puzzle be rotated clockwise or counter clockwise?
Please don't look at me as someone to give you the code you need - I'm not here for that, and neither is this forum. We're here to help YOU fix YOUR code.
Please list all the moves possible in your 8 tile puzzle.
Do you have a general outline of your program's functions yet? If so, post it, and let's get down to some specifics with your program.
I use Turbo C myself, for small programs.
Let me put your program into my compiler and be sure before I comment. You have put up a good deal of code, here.
The TTT game was a bust - it's not in C.
This is a pretty short and clear discussion of both DFS and BFS:
http://www.kirupa.com/developer/actionscript/depth_breadth_search.htm
Post up your linked list code (basic) program, as soon as you've gone as far as you can go with it.
I strongly suggest you get the linked list portion working right, FIRST, and then add the struct part to it, and all the details.
That's a BIG task!
I understand that, now. Do you understand that we are not a repository for that information?
Wikipedia has a lot of info on this, and Google has a ton, so do dig in. Here's a start:
http://en.wikipedia.org/wiki/Depth-first_search_with_iterative_deepening
note that there is ANOTHER entry on Wikipedia, just for regular DFS. I'm sure it has one for BFS, as well.
DFS (plain) is not guaranteed to find the solution with the fewest moves, whereas BFS always will. The beauty of DFS is that it needs a lot less memory to search a large space, and with ID added to it, it finds good shallow search tree answers, (maybe not ideal, but good). Overall DFS with ID is the best of the general searchers, imo.
I have some simple DFS code around. I'll post up something - may not be for a tile game, however - probably Tic-Tac-Toe.
Lots of info on Google for this - DFS is used in chess, checkers, almost exclusively, with ID or IID.
What we need from you, to pursue more details, is:
1) Code you have a question about or a problem, with.
2) That question.
If you can't provide #1 and #2 above, then this forum really can't help you out very much.
I'll post up that code as soon as I'm back from a meal.
OK. So you'll have your prototype for the struct above main(), (so it's global), and have a function to add another node to the list. In that function, you'll also have code to create one more instance of this struct.
So one node, holding one struct, for each student.
Singly linked or double linked list?
I would start with getting the linked list basics working OK - then add the struct stuff because that part of it involves a rather trivial amount of change to the code.
You only need to include struct members for fields that you need. For students:
idNum, fname, lname, gradYr, course, and DOB
(DOB=date of birth). You don't list it, but it's quite standard.
Course would be their major field of study perhaps.
Sounds like all you'll need, from your description.
Before the main() function, you'll prototype the struct - list it's fields and give each struct type, a name.
In main(), you'll declare the actual array of structs, so the data will be encapsulated, and given access only to the functions you send it to, as a parameter.
And whenever you post code, highlight it, and click on the [code] icon at the top of the editing window. Code tags around your program, make code easy to read, and easy to understand, and not all squished over to the left hand margin of the page.
Use code tags, or you'll be generally ignored!
P.S. Will you be using an array, or a linked list?
I'm not familiar with emulators, but suggest:
1) Use #defines for the memory addresses, to clarify things with them
2) Tell us exactly, *what* is it that has you stumped
"Needing help" doesn't bring a focus to anything specific enough to tackle. I doubt VERY MUCH if anyone is going to take your program, study it, and come back with a working program for you.
Right! We'll just need 4 instantiations of the coin class, and 4 more of the class dollar_bill. Then we'll need an inherited object of these two classes, for the bill and the change due.
You'd have to decide what will be private data, and what will be public data, for each of the above, and their relative worth assigned.
Then, at last, you'll get down to doing the very same arithmetic that you'd do in a procedural language! ;)
OOP is good for a lot of things, but it has no easy answers in this case.
I'm hesitant to say I can answer your questions - but I've done a lot of programming in Quickbasic Pro.
Post up what you have done, and what specifically you need help with. That's the only way that anyone could help you.
You might want to read up on base 2 (binary), base 16 (hexadecimal), and compare how they work, with what you know about base 10 (our normal number base).
What specifically is it that you need help with? Can you post code to show what you're trying to do?
And welcome to the forum! ;)
You're welcome, and I will. The first thing is to stop and think how YOU would make change - step by step - what is that process?
That's your pseudo code (logic outline), for the program.
Sorry Gameon, but I couldn't get char to work. /face palm
This is your program, in working trim:
#include <stdio.h>
int main() {
int i;
FILE *fin,*fout;
int c;
fin =fopen("test1.jpg","rb");
fout=fopen("test2.jpg","wb");
if(fin==NULL) {
printf("Error opening input file\n");
return 1;
}
if(fout==NULL) {
printf("Error opening output file\n");
return 1;
}
while((c=fgetc(fin))!=EOF)
fputc(c, fout);
fclose(fin);
fclose(fout);
printf("\n\n\t\t\t press enter when ready");
i=getchar(); ++i;
return 0;
}
You need to use ARRAYS of structs, of course - if you have multiple cars to track. ;)
If you're taking a class, use the compiler that the teacher will be testing you and your programs, with. There are two highly rated free C compilers that I know of. One is Microsoft's Visual Express (Google for a d/l site). The video is helpful to d/l as well.
The other is MingGW compiler, with Code::Blocks IDE. Google "Code::Blocks" for more info.
Turbo C won't handle newer C functions, or large memory demands, but it is simpler to use. It will easily handle simple graphics and non-buffered input, that both the above, will handle only with some difficulty.
Although I can't recommend Turbo C, I confess to using it with pleasure, for small stuff.
Narue has the pointer problem covered, below - typing slow, today.
Exactly right. Structs are used in C, to combine related variables, into one object.
This is a very old thread from 2009.
If you have a current problem like this, please start a new thread, and don't add to this one.
The "zombie's" need their rest. ;)
By convention, x and y denote a range of values in the horizontal and vertical direction respectively, on a plane. Taken together, they give the exact location of any point on that plane. Common name - coordinates or Cartesian coordinates.
So draw yourself a graph and label some x and y points on it. Now draw some rectanges - have some overlap (intersect), and others not overlap, and look at their coordinate values for x and y.
Look at the points where they intersect, and look at the points where they are not intersecting.
Hmmmmmmm! Something interesting there!! ;)
Draw yourself the graph of these x and y values, and the rectangles and points. Seeing it on paper is a big help.
A text file will be given certain translations (like a newline char gets changed into a CR/LF combination of two chars).
Open the files in binary mode: "rb" and "wb", for sure. Then you *may* need to change char c to an unsigned char.
And on the forum, highlight your code, and click on the [code] tag icon at the top of the editing window. That makes your code stay formatted properly, otherwise it all gets squished over to the far left hand margin and has the wrong font, also.
1) check that the customer has given you an amount exceeding his bill
2) use this equation: amount given - bill equals change due back to the customer
imo the easy way to deal with money is to multiply it all by 100, in order to eliminate any amount less than one (pennies are now dollars). Then do all the math making your change, (a while (change > 0) loop with every denomination of change inside it, is perhaps easiest to understand), and finally, print out the final bills, and use %.2d to print the coins (which you will have added up and know that they are less than 100 in value).
There is a sweet math way to do this, but it's harder to figure out.
Work on that, and post up your revised code. USE CODE TAGS AROUND YOUR PROGRAM CODE (highlight your code and click on the [code] icon in the editor window).
People won't study code that is all smashed up like html text.
Make your first two elements in a stack, and then compare the addresses they have.
To have your code look like code (and be easily read), highlight your code, and click on the [code] icon in the editing window.
You need the code tag without the backslash char: '/', before the program starts, and the code tag WITH the backslash char, placed after the program ends.
The above procedure will do it for you, automatically.
Welcome to the Forum! ;)
First idea, is to use a keyboard and editor, ;) but seriously, read up on permutations and programming in general. This is not a trivial "Hello World" kind of program.
Second idea, is to consider that hacking into someone's computer is a crime in most jurisdictions, and that your time and effort is much better spent, elsewhere.
Now that's enlightenment! ;)
Welcome to the forum, Akshay1.
This thread is 3 years old - how about starting a new one? More specifics of your assignment, would help. Also, what is stopping you from including string.h and using strstr() to find your string in the text file.?
click on the "Snippets" tab, and study the Binary Search snippet.
Your logic is *way* off the mark.
Unless the input is *strictly* formatted, don't use scanf(). You want a char buff[90] (nice and big) to hold a full line of input from the user or file, *PLUS* one newline: '\n' char and one end of string marker char: '\0'.
The format for it is:
char buff[90]={'\0'};
fgets(buff, sizeof(buff), stdin);
Where stdin could be the name of any input stream (like a file pointer even).
Now you'll take in a whole line up to the first <enter> key (which makes the newline, and thus the end of the line).
Now you can work your way through the buff array with sscanf() or whatever you want, as many times as you want, or need to.
Try that, and post up your problems, and we'll jump in to assist as needed. Trust me, it's THE way that you want to take in input from a user, when it's not going to be strictly formatted. (Which is most of the time, in R/L). ;)
And welcome to the forum! ;)
remove "- lensub from the for loop
add "and j<lenstr" to the while test condition
Logic works OK, but don't decrement j. Using another variable like j, inside the while loop, makes it a bit easier.
sujathaarsid, be *sure* your next post in this thread shows your additional work on this program, not just "help". :)
I would use logic like this:
[This is not "code", it's pseudo code, in C format. Difficult to describe it accurately, without code like syntax, imo.]
/* you can count these yourself, without using strlen(), if necessary */
lensub = strlen(substring);
lenstr = strlen(string);
for(i=0;i<(lenstr - lensub);i++) {
j=0;
while(string[i] == substring[j] {
j++;
i++;
}
/* now if --j == lensub you've found a substring so
break out of the for loop, or not, depending on
whether you want to find the first substring in the
string, or find all the substrings in the string.
decrement i, or you'll miss a letter in the string, if you're
searching for more substrings.
*/
}
Certainly not as easy as using strstr()! ;)
fire up the IDE, and click on "Options" then "Directories".
Check the listing for your header files. When you include the header file, be sure to add the < or " char's around the file name:
#include <stdlib.h>.
Works for default directory locations. "stdlib.h" works for limited searching for the header file.