944,143 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3868
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 26th, 2005
0

Please help me with arrays

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Jul 26th, 2005
0

Re: Please help me with arrays

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:
C++ Syntax (Toggle Plain Text)
  1. int box[10];
You can then put the thing in the fifth box like this:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < 10; i++ )
  2. std::cout<< box[i] <<'\n';
Or you could go from the other end:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 26th, 2005
0

Re: Please help me with arrays

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Jul 27th, 2005
0

Re: Please help me with arrays

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.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jul 27th, 2005
0

Re: Please help me with arrays

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 27th, 2005
0

Re: Please help me with arrays

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Jul 27th, 2005
0

Re: Please help me with arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 28th, 2005
0

Re: Please help me with arrays

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];
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Jul 28th, 2005
0

Re: Please help me with arrays

look up strcmp, memcmp, strncmp, etc...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 28th, 2005
0

Re: Please help me with arrays

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Moving an object up and down in C++!!
Next Thread in C++ Forum Timeline: Algebraic question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC