Please help me with arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Please help me with arrays

 
0
  #1
Jul 26th, 2005
I am taking a C++ class, but I am having a real trouble understanding arrays which confuse me completely.
This is the first time I take a programming class, so I really need help.

I do not really understand how arrays are read and filled or how to use for loops with them.
Please, if there is someone who has something that can help me, I'd really apprecciate it.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 729
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help me with arrays

 
0
  #2
Jul 26th, 2005
An array is just about the simplest and most useful thing you can think of. Imagine a line of boxes numbered 0 to N, where N is some arbitrary number. To put something in the fifth box, you walk over to the one with the number 4 (because they start at 0), and drop the thing in it. Then you walk back to the beginning of the row. To get the thing back, you walk back to the box with 4 and grab the thing.

Let's say that thing is a number, 123. The C++ code to create a line of ten boxes numbered 0 to 9 is:
  1. int box[10];
You can then put the thing in the fifth box like this:
  1. box[4] = 123;
Then to get the thing back, you do the same thing, except with a spare box on the left, which we'll call thing:
  1. int thing = box[4];
If you want every thing in the line of boxes, you go from one end to the other and grab the things in each box:
  1. for ( int i = 0; i < 10; i++ )
  2. std::cout<< box[i] <<'\n';
Or you could go from the other end:
  1. for ( int i = 9; i >= 0; i-- )
  2. std::cout<< box[i] <<'\n';
Just about any use of an array is some variation of the above.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please help me with arrays

 
0
  #3
Jul 26th, 2005
Thank you for your help.

However, when I try to use functions to make a program, i do not know how to use arrays.

For example, now i am trying to make a program that reads a file that contains integers and put them to an array, then the program has to read the numbers, and if there are two equal, it will give a warning to the user.

I am lost on how to find that duplicate!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Please help me with arrays

 
0
  #4
Jul 27th, 2005
To find a duplicate in the array, you have a few options.

One way is, make a for loop, looping the variable I from 0 to N. Then, inside that, make a loop that moves the variable J from I + 1 to N. Inside that, see if the I'th and J'th elements of the array are equal.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 729
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help me with arrays

 
0
  #5
Jul 27th, 2005
>when I try to use functions to make a program, i do not know how to use arrays
On the surface, nothing changes. You declare an array as a function parameter the same way. The only difference is that you don't need the size, and it's a good idea to pass the size as a separate parameter:
  1. #include <iostream>
  2.  
  3. void print_array ( int a[], int size )
  4. {
  5. for ( int i = 0; i < size; i++ )
  6. std::cout<< a[i] <<'\n';
  7. }
  8.  
  9. int main()
  10. {
  11. int a[] = {1,2,3,4,5};
  12.  
  13. print_array ( a, 5 );
  14. }
>I am lost on how to find that duplicate!
Rash's suggestion is the simplest (and you should use it for now), but far from the most efficient. Just as a taste of the other options, you can sort the array and then check adjacent values to see if they're the same, or you can use an existence table:
  1. #include <iostream>
  2.  
  3. template <typename T, int sz>
  4. char (&array(T(&)[sz]))[sz];
  5.  
  6. void print_duplicates ( int a[], int size )
  7. {
  8. int exist[10] = {0};
  9.  
  10. for ( int i = 0; i < size; i++ )
  11. ++exist[a[i]];
  12.  
  13. for ( int i = 0; i < sizeof array ( exist ); i++ ) {
  14. if ( exist[i] > 1 )
  15. std::cout<< i <<" is duplicated "
  16. << exist[i] - 1 <<" times\n";
  17. }
  18. }
  19.  
  20. int main()
  21. {
  22. int a[] = {5,4,7,6,5,6,7,8,9,3,4,5,6,7,8};
  23.  
  24. print_duplicates ( a, sizeof array ( a ) );
  25. }
An existence table is an array of counters, where the value in the original array is the index. By using a[0] as the index for exist, exist[5] is incremented. This is not an good method when the possible ranges are big because you need an array that can hold every possible value, but other methods (such as using a std::map can be generalized quite nicely, though you lose the linear performance.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please help me with arrays

 
0
  #6
Jul 27th, 2005
Thank your very much for all your help, my program worked with the for loops. After getting the duplicates, I also had to put those integers in order, but I have done it already.

However, I have another question,
How do you compare a word with itself to check if it is a palindrome?

Do I have to use a character array or a string array because the user has to give me that word and the program has to read it backwards to check if it is or not?

If anyone has an idea please let me know!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 729
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Please help me with arrays

 
0
  #7
Jul 27th, 2005
>How do you compare a word with itself to check if it is a palindrome?
This is a classic problem, and you already have the tools to solve it. Give it a shot and see what you can come up with.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please help me with arrays

 
0
  #8
Jul 28th, 2005
I am trying to work on this problem. I am assuming the word I am going to check is madam, but I am not sure how to compare them because my book says it is not possible to compare character arrays.

Like say word1[10], word2[10], not possible to say
if (word1==word2)

This is what i have now for entering the word and outputing it backwards, but then I am not sure how to compare them.

const int size = 5;
char pal[size];
cout<<"Enter a word:"<<endl;
cin>>pal;

for(int n=size; n>=0; n--)
cout<<pal[n];
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Please help me with arrays

 
0
  #9
Jul 28th, 2005
look up strcmp, memcmp, strncmp, etc...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Please help me with arrays

 
0
  #10
Jul 28th, 2005
You can check for palindrome(ness) using many different techniques. You could reverse the entire string and compare the original string with the reverse to see if it is the same, but that requires knowledge of how to compare strings (winbatch has indicated some of the ways to do that). An alternative is to realize that a C style string is a null terminated char array. A palindrome is a word (or group of words), think string, that is spelled the same forward as backward. That means the first and last letter, think char, of a palindrome are the same, the second and second to last letter are the same, etc. So what? Well, you can determine the length of a word by counting the letters. You can determine the length of a string by counting the char, or, if you are familiar with strlen(), you can use it to determine the length of a C style string. strlen() returns the number of non-null char in a C style string. Once you know the length of the string you can determine what int is the largest number that is less than one half the length of the string. Then use a loop to look at each char in the first half of the string in ascending order and compare it with each char in the second half of the string starting from the end of the string and working back toward the middle. If each comparison is a match, then the string is a palindrome. The first time you have a mismatch you know the word/phrase/string isn't a palindrome and you can break out of the comparison.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC