User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,749 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,738 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2899 | Replies: 16
Reply
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Please help me with arrays

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,340
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 29
Solved Threads: 460
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Please help me with arrays

  #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:
int box[10];
You can then put the thing in the fifth box like this:
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:
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:
for ( int i = 0; i < 10; i++ )
  std::cout<< box[i] <<'\n';
Or you could go from the other end:
for ( int i = 9; i >= 0; i-- )
  std::cout<< box[i] <<'\n';
Just about any use of an array is some variation of the above.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please help me with arrays

  #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  
Join Date: Jun 2005
Location: Troy
Posts: 1,277
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Please help me with arrays

  #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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,340
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 29
Solved Threads: 460
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Please help me with arrays

  #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:
#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 );
}
>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:
#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 ) );
}
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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please help me with arrays

  #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  
Join Date: Sep 2004
Posts: 6,340
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 29
Solved Threads: 460
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Please help me with arrays

  #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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please help me with arrays

  #8  
Jul 27th, 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  
Join Date: Feb 2005
Posts: 462
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Please help me with arrays

  #9  
Jul 28th, 2005
look up strcmp, memcmp, strncmp, etc...
Reply With Quote  
Join Date: Jul 2005
Posts: 1,157
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 152
Lerner Lerner is offline Offline
Veteran Poster

Re: Please help me with arrays

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC