Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem in Array in c++

Status
Not open for further replies.

dreamgirl

MIS
Nov 25, 2002
4
AE
i have this problem i done the coding but it is not working can some one guide me where is the mistake

Write a program that declares an array Alpha of 50 components of the type float. 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 three times the index variable. Out put that array so that 10 elements per line are displayed on the screen.

Float Alpha[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25};
Float Index, Square;
Index=0;
Square=2*index;

While (index<=25)
{
Alpha[Index]= =Square;
Index++;
}

Float Alpha []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25};
Float Index, Times;
Index=0;
Times= Index*3;
While (index<=25)
Alpha[index]=Times;
Index++;
}
cout<<”the 10 Array elements are:”;
cin>>Alpha[1]>>Alpha[2] >>Alpha[3]>>Alpha[4]>>Alpha[5]>>Alpha[6] >>Alpha[7] >>Alpha[8] >>Alpha[9] >>Alpha[
 
Alpha[Index]= =Square;

Trash this line. Alpha[Index]==Square is a comparison, not an assignment.

Square=2*index;

Trash the square variable. It isn't needed, plus you initialize it with Square=2*0; because index == 0 at that point, and this value never changes.

Instead, do this:

const int elementNum = 25; //number of elements in array

for( index = 0, index < elementNum, index++ )
Alpha[index] = index * 2; //or (index + 1) * 2 depending on your wants

Note this also initializes the Alpha array elements, so instead of declaring Alpha like:

Float Alpha[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25};

You can instead declare:

Float Alpha[elementNum];

then enter the for loop.

Don't redeclare Alpha, in the second part of your procedure, do somthing similar for the second set of calculations, and you're good to go.

Also, you might want to concider using a for loop in the printing to simplify things. Use the % operator to insert a newline character for every 10 elements. % gives you the remainder after division. E.G. 5/2 == 2 with a remainder of 1 so 5 % 2 == 1

for( index = 0, index < elementNum, index++ )
{
if( index % 10 == 0 ) //every tenth row
cout << '\n';
cout << Alpha[index];
}

Tell me if you want to know more.


 
Hi,
Some of things in code are used wrong like
Alpha[Index]= =Square; as this is a comparison situation while it is used as an assignment.. I think that the requiremnt is to print only first 10 characters..If I am getting it right then i think you meant something like this

#include &quot;stdafx.h&quot;
#include <iostream.h>

int main(int argc, char* argv[])
{

float fArr[50];
float fIndex =3.0;
int iCntr=0,iTmpCntr=0;

while(iCntr < 25)
{
fArr[iCntr]=2*fIndex;
iCntr++;
}

while(iCntr >= 25 && iCntr < 50)
{
fArr[iCntr]=3*fIndex;
iCntr++;
}

iTmpCntr=iCntr;
iCntr=0;
while(iCntr < 10)
{
cout<<iCntr <<&quot;-&quot;<< fArr[iCntr]<<&quot;\t&quot;;
iCntr++;
}
return 0;
}
If there is any confusion regarding this let me know

 

#include <iostream>

using std::cout;
using std::endl;


int main(int argc, char* argv[])
{
const ARRSIZE = 50;
const ROWSIZE = 10;

unsigned j;

cout << &quot;** ----- works without array declaration: &quot; << endl;
for (j = 1; j <= ARRSIZE; j++)
{
if (j <= 25)
cout << j * j << '\t';
else
cout << j * 3 << '\t';
if (j % ROWSIZE == 0)
cout << endl;
}

cout << &quot;** ----- with array declaration: &quot; << endl;
// ** create and populate the array
float fArr[ARRSIZE];
for (j = 1; j <= ARRSIZE; j++)
{
if (j <= 25)
fArr[j-1] = j * j;
else
fArr[j-1] = j * 3;


}
// ** now output that array.
for (j = 1; j <= 50; j++)
{
cout << fArr[j-1] << '\t';
if (j % ROWSIZE == 0) // ** modulus - only print 10 values per row.
cout << endl;
}
return 0;
}

Good Luck, ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top