943,548 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4082
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 28th, 2007
0

arrays proplem

Expand Post »
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 ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Dec 28th, 2007
0

Re: arrays proplem

>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:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < 25; i++ )
  2. alpha[i] = i * i;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 28th, 2007
0

Re: arrays proplem

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.
C++ Syntax (Toggle Plain Text)
  1. int alpha[50];
  2. int i = 0;
  3. for(i=0;<50;++i)
  4. {
  5. cout<<alpha[i]<<" ";
  6. }

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.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 28th, 2007
0

Re: arrays proplem

I thought this was initialization:

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

and this:
C++ Syntax (Toggle Plain Text)
  1. int num[4];
  2. for(int i = 0; i < 4; i++i)
  3. 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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 28th, 2007
0

Re: arrays proplem

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 7:22 pm.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 29th, 2007
0

Re: arrays proplem

before every thing thank you for helping , and Im asking if these answer is right :
C++ Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. using namespace std ;
  3. int main ()
  4. {
  5. int alpha[50];
  6. for (int i=0;i<25;i++)
  7. {
  8. alpha[i]=i*i;
  9. cout<<alpha[i]<<" ";
  10. for(i=25;i<50;i++)
  11. {
  12. alpha[i]=3*i;
  13. cout<<alpha[i]<<" ";
  14. }
  15. return 0
  16. }
also I have question how can I print 10 elements per line if I had 2 loop
and thank you very much ,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Dec 29th, 2007
0

Re: arrays proplem

before every thing thank you for helping , and Im asking if these answer is right :
C++ Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. using namespace std ;
  3. int main ()
  4. {
  5. int alpha[50];
  6. for (int i=0;i<25;i++)
  7. {
  8. alpha[i]=i*i;
  9. cout<<alpha[i]<<" ";
  10. for(i=25;i<50;i++)
  11. {
  12. alpha[i]=3*i;
  13. cout<<alpha[i]<<" ";
  14. }
  15. return 0
  16. }
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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 29th, 2007
0

Re: arrays proplem

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.

C++ Syntax (Toggle Plain Text)
  1. for(int i =0; i<25; i++) {
  2. alpha[i] = i * i;
  3. alpha[49-i] = (49-i) * 3;
  4. }
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Dec 30th, 2007
0

Re: arrays proplem

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 .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Dec 30th, 2007
0

Re: arrays proplem

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)
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: direct access to hdd
Next Thread in C++ Forum Timeline: how to choos a path





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


Follow us on Twitter


© 2011 DaniWeb® LLC