954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

arrays proplem

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 ?

bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

>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:

for ( int i = 0; i < 25; i++ )
  alpha[i] = i * i;
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

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 ,

bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.

superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
 
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;
}
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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 .

bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 
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)

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 
for(int i=0 ; i<50 ; i++)
{cout<<a[i]<<" ";
if(i%10 == 0)
cout<<endl;
}

you had to use cout<<endl if i is divisble by 10 i.e. 10 elements has been printed..

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
 

it will print 11 elements in first line and 10 in all others...
to be more accurate use...

for(int i=1 ; i<51 ; i++)
    {
            
            cout<<setw(3)<<a[i-1]<<" ";
            if(i%10 == 0)
            cout<<endl;
    }
rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
 
for(int i=0 ; i<50 ; i++)
{cout<<a[i]<<" ";
if(i%10 == 0)
cout<<endl;
}

you had to use cout<<endl if i is divisble by 10 i.e. 10 elements has been printed..


It should be:

if( (i+1)%10 == 0)

Becausei was started from 0

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

this is my answer ; is it right ?

# include <iostream>
#include <iomanip >
using namespace std ;
int main ()
{
	
int alpha[50];
for (int i=0;i<25;i++)
{
alpha[i]=i*i;
cout<<alpha[i]<<" ";
}
for(int i=25;i<50;i++)
{
alpha[i]=3*i;
cout<<alpha[i]<<" ";
}
for(int i=1 ; i<51 ; i++)    {                           
    {
            
            cout<<setw(3)<<alpha[i-1]<<" ";
            if( (i+1)%10 == 0)
            cout<<endl;
    }
    
}

return 0;
}
bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This works if you don't mind the newline right from the get go.

if (i % 10 == 0)
   cout << endl;
cout << alpha[i] << "\t";
superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
 
this is my answer ; is it right ?

I'd guess that you should be able to determine if it's right or not and I'm suspecting you're guessing it's not.

I'd suggest removing the cout statements from the for loops of the assignment statements, they just print 25 across the line, which is not what you want.

I removed those cout statements and your code is nearly there. The first line only prints 9 numbers, the rest print 10.

superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
 
for(int i=1 ; i<51 ; i++)    {                           
    {
            
            cout<<setw(3)<<alpha[i-1]<<" ";
            if( (i+1)%10 == 0)
            cout<<endl;
    }
    
}

Double open brace and double close brace?

There are 2 ways to choose to print 10 numbers for each line.

for (int i=0; i<50; i++) {
   cout << alpha[i] << "  ";
    if ((i+1) % 10 == 0)
        cout << endl;
}

Or

for (int i=1; i<51; i++) {
   cout << alpha[i-1] << "  ";
    if (i % 10 == 0)
        cout << endl;
}

This one also work too

for (int i=0; i<50; ) {
   cout << alpha[i] << "  ";
    if (++i % 10 == 0)
        cout << endl;
}
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You