|
#1
|
|||
|
|||
|
Array and using two sub functions
Here is the problem:
Write a C++ program that declares an array alpha of 50 components of type double Initalize 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. Output the array so that 10 elements per line are printed. Here is my first part of the problem: #include <iostream> #include <iomanip> #include <conio.h> using namespace std; int main() { double alpha [50]; int i; for (i=0; i < 25; i++) { alpha [i] = i * i; } for (i=25; i < 50; i++) { alpha [i] = 3 * i; } for(i = 0; i < 50; i++) { cout << setw(4) << alpha[i]; if( (i + 1) % 10 == 0 ) cout << endl; } getch(); return 0; } Now Part B saids: Revise your solution of the above exercise using sub functions. Use two sub-functions. The array is created in the main. 1. Function initialize takes the array and initialize to the specified values. 2. Function display takes the array and display it's content, 10 elements per line. The display function should not be able to modify the array contents. I dont know how to do part B Please help. |
|
#2
|
|||
|
|||
|
void foo(some_type *stp, int size)
/*you pass the array as a pointer, and you have to pass its size as well or your subroutine cannot know the size. You can make the size a global constant or something but better to just pass it along. */ { for(int i = 0; i < size; i++) { stp[i] = 0; //initialize to zero for example } } int main() { some_type array[max_size]; foo(array, max_size); } The other routine will work the same way, pass the array and its size and then print the data as you have done in the past. You have to use the const keyword here to prevent your routine from changing the array, in order to make your teacher happy (the compiler doesnt care, if you dont modify it, it will not change). |
|
#3
|
|||
|
|||
|
Thank you for the help BUT
I finished this project but please take a look at my new one that I started I will post it as a new thread.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|