Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > C++

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 11-04-2009, 12:00 PM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
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.
Reply With Quote
  #2  
Old 11-04-2009, 12:29 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
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).
Reply With Quote
  #3  
Old 11-04-2009, 04:12 PM
fantasyfootball fantasyfootball is offline
Registered User
 
Join Date: Oct 2009
Posts: 9
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.
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:00 PM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.