| | |
arrays proplem
Thread Solved
![]() |
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 ?
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 ?
>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:
>components are equal to the square of the index variable ?
I would assume something like this:
C++ Syntax (Toggle Plain Text)
for ( int i = 0; i < 25; i++ ) alpha[i] = i * i;
I'm here to prove you wrong.
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.
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
I thought this was initialization:
int num[] = {1, 2, 3, 4};
and this:
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.
int num[] = {1, 2, 3, 4};
and this:
C++ Syntax (Toggle Plain Text)
int num[4]; for(int i = 0; i < 4; i++i) num[i] = i;
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.
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.
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.
I know I am. Therefore I am.
before every thing thank you for helping , and Im asking if these answer is right :
also I have question how can I print 10 elements per line if I had 2 loop
and thank you very much ,
C++ Syntax (Toggle Plain Text)
# 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 }
and thank you very much ,
•
•
•
•
before every thing thank you for helping , and Im asking if these answer is right :
also I have question how can I print 10 elements per line if I had 2 loopC++ Syntax (Toggle Plain Text)
# 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 }
and thank you very much ,
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.
•
•
•
•
You're on the right track. Another solution would be to use only onefor loopand use anif elsetest condition to test for the first 25, do whatever...
C++ Syntax (Toggle Plain Text)
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
Behind every smile is a tear.
Visal .In
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
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: direct access to hdd
- Next Thread: how to choos a path
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






