| | |
Copying words into an array of char ?!?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I GIVE UP !!!!!
Ive been trying to program for over two years now, have been reading several books, did all exercises, but for some unknown reason, I'm not getting it.
So, after some thought about it, Ive decided to stop, I want to thank all of you who have helped me the past two years here on Daniweb and wish you all the best.
Kind regards,
Johan aka JoBe
Ive been trying to program for over two years now, have been reading several books, did all exercises, but for some unknown reason, I'm not getting it.
So, after some thought about it, Ive decided to stop, I want to thank all of you who have helped me the past two years here on Daniweb and wish you all the best.
Kind regards,
Johan aka JoBe
*sigh* We really will miss you JoBe, though I wish you hadn't given up.
Your friend always,
~s.o.s~
Your friend always,
~s.o.s~
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Thanks for the kind words s.o.s, but, I can't stop untill I know the answer, so if someone would please help me out with this, Ive been trying and I can't find the solution.
The code Ive got is this:
But, if I use the input:
one
two
one
four
five
The output is:
one
What am I doing wrong
Getting so frustrated by not finding this solution :mad: And it is probably something simple that I can't see !!!!!!
The code Ive got is this:
C Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> int main() { char wordArr[5][10]; bool contr = false; size_t i, j; for(i = 0; i < 5; i++) std::cin >> wordArr[i]; for(i = 0; i < 5; i++) { for(j = 0; j < i; j++) { contr = strcmp(wordArr[i], wordArr[j]); if (contr == true) contr = true; } if (contr == false) std::cout << wordArr[i] << '\n'; } std::cin.ignore(2); return 0; }
But, if I use the input:
one
two
one
four
five
The output is:
one
What am I doing wrong
Getting so frustrated by not finding this solution :mad: And it is probably something simple that I can't see !!!!!!
Last edited by JoBe; Dec 3rd, 2006 at 5:19 am.
#include <iostream>
#include <cstring>
int main()
{
char wordArr[5][10];
bool contr = false;
size_t i, j;
for(i = 0; i < 5; i++)
std::cin >> wordArr[i];
for(i = 0; i < 5; i++)
{
for(j = 0; j < i; j++)
{
contr = strcmp(wordArr[i], wordArr[j]);
// If the above line sets contr to true or false...
if (contr == true) // what are these two lines for?
contr = true;
}
if (contr == false)
std::cout << wordArr[i] << '\n';
}
std::cin.ignore(2);
return 0;
} Last edited by WaltP; Dec 3rd, 2006 at 5:43 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Hi Walt,
Well, Lerner posted this helpfull piece of pseudo code:
And I thought, oh, now I get it:
-First I have a loop for the whole array.
-Then, I have a loop for the word that is being checked whether it exists allready.
-Then, if that word is the same one of the allready added words in the first loop, put the bool variable to true, if not, at the end of looping threw the second loop. Output the word, since it hasn't been added yet.
The idea of the exercise now is to print every word only once, meaning:
one
two
one
four
five
would have to print:
one
two
four
five
Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
Well, Lerner posted this helpfull piece of pseudo code:
C Syntax (Toggle Plain Text)
bool found = false for each word in the array //this word for each word prior to this word //current word if this word is the same as current word set found to true if not found print out this word
And I thought, oh, now I get it:
-First I have a loop for the whole array.
-Then, I have a loop for the word that is being checked whether it exists allready.
-Then, if that word is the same one of the allready added words in the first loop, put the bool variable to true, if not, at the end of looping threw the second loop. Output the word, since it hasn't been added yet.
The idea of the exercise now is to print every word only once, meaning:
one
two
one
four
five
would have to print:
one
two
four
five
Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
•
•
•
•
Hi Walt,
Well, Lerner posted this helpfull piece of pseudo code:
C Syntax (Toggle Plain Text)
bool found = false for each word in the array //this word for each word prior to this word //current word if this word is the same as current word set found to true if not found print out this word
And I thought, oh, now I get it:
-First I have a loop for the whole array.
-Then, I have a loop for the word that is being checked whether it exists allready.
-Then, if that word is the same one of the allready added words in the first loop, put the bool variable to true, if not, at the end of looping threw the second loop. Output the word, since it hasn't been added yet.
The idea of the exercise now is to print every word only once, meaning:
one
two
one
four
five
would have to print:
one
two
four
five
Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
*Voted best profile in the world*
•
•
•
•
Hint: wouldn't it be a lot easier if you sorted the current words into alphabetical order first.
- Read a sequence of words from input. Use Quit as a word that therminates input. Print the words in the order they were entered. Don't print a word twice. Modify the program to sort the words before printing them.
So,
1) Read a sequence of words from input.
2) Use Quit as a word that terminates input.
3) Print the words in the order they were entered.
4) Don't print a word twice.
And
5) Modify the program to sort the words before printing them.
I could try what you suggest, but, then I won't be doing it in the order the exercise says, probably for the reason that N°4 would become easier

Anyway, thanks for the tip iamthwee
Last edited by JoBe; Dec 3rd, 2006 at 9:34 am.
>I could try what you suggest, but, then I won't be doing it in the order the exercise says, probably for the reason that N°4 would become easier
It certainly would be a lot easier. Which is how most hash tables operate I would imagine. I.e sorting the list first before removing duplicates.
Of course after sorting you lose the order you begin with.
It certainly would be a lot easier. Which is how most hash tables operate I would imagine. I.e sorting the list first before removing duplicates.
Of course after sorting you lose the order you begin with.
*Voted best profile in the world*
•
•
•
•
Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
What Mr. Lerner tried to explain is that if you start from the start of the array, you would have a hard time keeping track of duplicates.
Eg. one, two, one, two, three
If you begin at the start of the array you have no way of knowing that the current word under consideration would be repeated or not i.e. if we consider the first word "one" we have no way of knowing that "one" would occur again or not.
The only logical thing to beat this problem would be to start from the end of the array, eg. "three", look up for it in the whole array, and see if it occurs anywhere in the whole array. If it does, don't print "three" because we know for sure that we will encounter "three" again definately.
•
•
•
•
A sample run for input "one, two, one, two, three"
Current Word: three
Searching upwards for "three"...not found
Print "three"
Current word: two
Searching upwards for "two"...found
Dont print "two"
Current word: one
Searching upwards for "one"...found
Dont print "one"
Current word: two
Searching upwards for "two"...not found
Print "two"
Current word: one
Searching upwards for "one"...not found
Print "one"

Hope it helped, repost if necessary.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Okay. Here this goes.
What Mr. Lerner tried to explain is that if you start from the start of the array, you would have a hard time keeping track of duplicates.
•
•
•
•
I won't let you give up so easily
Programming is sometimes so frustrating, but, I can't seem to let it go, seems like Ive been bitten by the microbe, problem is, my brains don't seem to be willing to help out here. Glad that this is merely a hobby of mine and I don't have to earn a living with it, wouldn't be earning alot with it :cheesy: •
•
•
•
Hope it helped, repost if necessary.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Linker info
- Next Thread: Deleting characters function ( with just one parameter)
Views: 7144 | Replies: 31
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax directory dynamic fflush file fork forloop framework frequency functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest homework inches incrementoperators kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault sequential shape socket socketprograming spoonfeeding stack standard string strings structures student systemcall threads turboc unix user variable voidmain() wab win32 windows.h






