Copying words into an array of char ?!?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #11
Dec 2nd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Copying words into an array of char ?!?

 
0
  #12
Dec 2nd, 2006
*sigh* We really will miss you JoBe, though I wish you hadn't given up.

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #13
Dec 3rd, 2006
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:

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int main()
  5. {
  6. char wordArr[5][10];
  7. bool contr = false;
  8. size_t i, j;
  9.  
  10. for(i = 0; i < 5; i++)
  11. std::cin >> wordArr[i];
  12.  
  13. for(i = 0; i < 5; i++)
  14. {
  15. for(j = 0; j < i; j++)
  16. {
  17. contr = strcmp(wordArr[i], wordArr[j]);
  18.  
  19. if (contr == true)
  20. contr = true;
  21. }
  22. if (contr == false)
  23. std::cout << wordArr[i] << '\n';
  24. }
  25.  
  26. std::cin.ignore(2);
  27.  
  28. return 0;
  29. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Copying words into an array of char ?!?

 
0
  #14
Dec 3rd, 2006
#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;
}
What is it you are trying to do?
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #15
Dec 3rd, 2006
Hi Walt,


Well, Lerner posted this helpfull piece of pseudo code:
  1. bool found = false
  2. for each word in the array //this word
  3. for each word prior to this word //current word
  4. if this word is the same as current word
  5. set found to true
  6. if not found
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Copying words into an array of char ?!?

 
0
  #16
Dec 3rd, 2006
Originally Posted by JoBe View Post
Hi Walt,


Well, Lerner posted this helpfull piece of pseudo code:
  1. bool found = false
  2. for each word in the array //this word
  3. for each word prior to this word //current word
  4. if this word is the same as current word
  5. set found to true
  6. if not found
  7. 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.
Hint: wouldn't it be a lot easier if you sorted the current words into alphabetical order first.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #17
Dec 3rd, 2006
Originally Posted by iamthwee View Post
Hint: wouldn't it be a lot easier if you sorted the current words into alphabetical order first.
Probably, but the exercise goes as follows:

- 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Copying words into an array of char ?!?

 
0
  #18
Dec 3rd, 2006
>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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Copying words into an array of char ?!?

 
0
  #19
Dec 3rd, 2006
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.
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.

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"
I won't let you give up so easily

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #20
Dec 3rd, 2006
Originally Posted by ~s.o.s~ View Post
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.
Ah, ok, let me see whether I can find a solution that way.


I won't let you give up so easily
Thanks for sticking in s.o.s 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.
I most certainly will s.o.s, but, it'll probably will be next week since during the week, I don't have time, full time job and other responcebilities.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 7144 | Replies: 31
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC