RSS Forums RSS

arrays proplem

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 16
Reputation: bis student is an unknown quantity at this point 
Solved Threads: 0
bis student's Avatar
bis student bis student is offline Offline
Newbie Poster

Help arrays proplem

  #1  
Dec 28th, 2007
hello ,Im a new member and Im not very good in english , so please excuse me , I have question :
write a C++ program that declares an array alpha of 50 component of type double . Initialize the array so that the first 25 components are equal to the square of the index variable , and the last 25 components are equal to the three times the index variable . output the array so that 10 elements per line are printed .

what does it this mean Initialize the array so that the first 25 components are equal to the square of the index variable ?
AddThis Social Bookmark Button
Reply With Quote  
Posts: 7,460
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: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: arrays proplem

  #2  
Dec 28th, 2007
>what does it this mean Initialize the array so that the first 25
>components are equal to the square of the index variable ?
I would assume something like this:
  1. for ( int i = 0; i < 25; i++ )
  2. alpha[i] = i * i;
I'm here to prove you wrong.
Reply With Quote  
Posts: 174
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: arrays proplem

  #3  
Dec 28th, 2007
Once you declare an array you just reserve some amount of memory. This memory contains garbage. if you try to print all values of the array just after delaring it you will see just garbage. See folowing line of code.
int alpha[50];
int i = 0;
for(i=0;<50;++i)
{
    cout<<alpha[i]<<"  ";
}

Initialization means providing valid values to elements for the first time.
Narue's code intializes first 25 elements of the array. You can try initializing rest.
I know I am. Therefore I am.
Reply With Quote  
Posts: 1,572
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: 239
Lerner Lerner is offline Offline
Posting Virtuoso

Re: arrays proplem

  #4  
Dec 28th, 2007
I thought this was initialization:

int num[] = {1, 2, 3, 4};

and this:
int num[4];
for(int i = 0; i < 4; i++i)
  num[i] = i;
represented serial assignment.

Furthermore I think that either the instructor, the OP, or I (and I don't think it's me this time) don't know the difference between iniatialization of the elements of an array and assignment to the elements of an array and use the two concepts interchangeably, as they can end up with the same result.
Reply With Quote  
Posts: 174
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: arrays proplem

  #5  
Dec 28th, 2007
I believe we are confused in Initialization and declaration.

int i ; // declaration
int i = 10 ; declaration + initialization

i= 15 ; // assignment

You cannot only do initialization without declaration.

As per the initial question is concerned it is stupid to initialize the array with different values.

int alpha [50] = {1,2*2,3*3,…………………} ;// wrong syntax

int alpha[50] = {0} ; //initialize all the fifty elements by 0

Naure’s solution presumes taht the instructor wants to *assign* values to the array not exactly initialize. I agree with the solution.
Last edited by dubeyprateek : Dec 28th, 2007 at 6:22 pm.
I know I am. Therefore I am.
Reply With Quote  
Posts: 16
Reputation: bis student is an unknown quantity at this point 
Solved Threads: 0
bis student's Avatar
bis student bis student is offline Offline
Newbie Poster

Re: arrays proplem

  #6  
Dec 29th, 2007
before every thing thank you for helping , and Im asking if these answer is right :
# include <iostream>
using namespace std ;
int main ()
{
int alpha[50];
for (int i=0;i<25;i++)
{
alpha[i]=i*i;
cout<<alpha[i]<<" ";
for(i=25;i<50;i++)
{
alpha[i]=3*i;
cout<<alpha[i]<<" ";
}
return 0
}
also I have question how can I print 10 elements per line if I had 2 loop
and thank you very much ,
Reply With Quote  
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: arrays proplem

  #7  
Dec 29th, 2007
Originally Posted by bis student View Post
before every thing thank you for helping , and Im asking if these answer is right :
# include <iostream>
using namespace std ;
int main ()
{
int alpha[50];
for (int i=0;i<25;i++)
{
alpha[i]=i*i;
cout<<alpha[i]<<" ";
for(i=25;i<50;i++)
{
alpha[i]=3*i;
cout<<alpha[i]<<" ";
}
return 0
}
also I have question how can I print 10 elements per line if I had 2 loop
and thank you very much ,

You're on the right track. Another solution would be to use only one for loop and use an if else test condition to test for the first 25, do whatever...

As regards printing 10 per line, you could make use of the % modulus operator to determine whether a newline should be printed if (i % 10 == 0) .

I hope this is of value.
Reply With Quote  
Posts: 419
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Solved Threads: 39
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: arrays proplem

  #8  
Dec 29th, 2007
Originally Posted by superjacent View Post
You're on the right track. Another solution would be to use only one for loop and use an if else test condition to test for the first 25, do whatever...

If you want to use only one loop, you don't need to use condition.

for(int i =0; i<25; i++) {
   alpha[i] = i * i;
   alpha[49-i] = (49-i) * 3;
}
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Posts: 16
Reputation: bis student is an unknown quantity at this point 
Solved Threads: 0
bis student's Avatar
bis student bis student is offline Offline
Newbie Poster

Re: arrays proplem

  #9  
Dec 30th, 2007
thank you again for helping , and I want another hint about print 10 elements per line because this if (i % 10 == 0) does is not work .
there are always hope
Reply With Quote  
Posts: 419
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Solved Threads: 39
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: arrays proplem

  #10  
Dec 30th, 2007
Originally Posted by bis student View Post
thank you again for helping , and I want another hint about print 10 elements per line because this if (i % 10 == 0) does is not work .

After you successfully initialize your array, you create another loop for print each element of your array. Every 10 elements, you insert the newline character. (Hint: everytimes the counter is divisable by 10, then it means you have already print 10 elements in that line)
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Views: 2634 | Replies: 16 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:14 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC