| | |
Please help me with arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Solved Threads: 0
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.
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.
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:
You can then put the thing in the fifth box like this:
Then to get the thing back, you do the same thing, except with a spare box on the left, which we'll call thing:
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:
Or you could go from the other end:
Just about any use of an array is some variation of the above.
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)
int box[10];
C++ Syntax (Toggle Plain Text)
box[4] = 123;
C++ Syntax (Toggle Plain Text)
int thing = box[4];
C++ Syntax (Toggle Plain Text)
for ( int i = 0; i < 10; i++ ) std::cout<< box[i] <<'\n';
C++ Syntax (Toggle Plain Text)
for ( int i = 9; i >= 0; i-- ) std::cout<< box[i] <<'\n';
I'm here to prove you wrong.
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Solved Threads: 0
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!
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!
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.
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.
>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:
>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:
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.
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)
#include <iostream> void print_array ( int a[], int size ) { for ( int i = 0; i < size; i++ ) std::cout<< a[i] <<'\n'; } int main() { int a[] = {1,2,3,4,5}; print_array ( a, 5 ); }
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)
#include <iostream> template <typename T, int sz> char (&array(T(&)[sz]))[sz]; void print_duplicates ( int a[], int size ) { int exist[10] = {0}; for ( int i = 0; i < size; i++ ) ++exist[a[i]]; for ( int i = 0; i < sizeof array ( exist ); i++ ) { if ( exist[i] > 1 ) std::cout<< i <<" is duplicated " << exist[i] - 1 <<" times\n"; } } int main() { int a[] = {5,4,7,6,5,6,7,8,9,3,4,5,6,7,8}; print_duplicates ( a, sizeof array ( a ) ); }
I'm here to prove you wrong.
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Solved Threads: 0
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];
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];
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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.
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Moving an object up and down in C++!!
- Next Thread: Algebraic question
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






