Question:
c++ help, printing arrays w/ default values?
deltemis
2010-01-26 15:24:04 UTC
i'm to write 3 overloaded functions that each will print an array. one prints ints, another prints floats, and last prints chars. i'm to initialize the arrays to any values and then write the testing calls for each printing function. i need to have at least 1 call use the default argument and another one that isn't default.

code so far (a lil buggy, kinda stumped):
#include
#include
using namespace std;

int main()
{
void printi(const int data[], int size = 10); //int ver
void printfl(const float data[], int size = 10); //float ver
void printc(const char data[], int size = 10); //char ver

int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
float b[] = {.1, .2, .3, .4, .5, .6, .7, .8, .9};
char c[] = "a, b, c, d, e, f, g, h, i";

printi(a);
printfl(b);
printc(c);

}

void print(const int data[], int size = 10) //int ver
{
int i, amount = 0, x = 10;
for (i = 0; x > i; i++)
cout << data[i];
}

void printfl(const float data[], int size = 10) //float ver
{
int i, x = 10;
float amount = 0;
for (i = 0; x > i; i++)
cout << data[i];
}

void printc(const char data[], int size = 10) //char ver
{
int i, amount = 0, x = 10;
for (i = 0; x > i; i++)
cout << data[i];
}

overall i do not understand fully what i am supposd to do programming wise. i believe i have initiated the arrays to values properly but what's getting me is how do i make the printing functions work and how do i test them for default/non default values?

a classmate gave me an example for test calls from his program and he used

print(a);
print(b, 5);
print(c);

he used those calls to test his functions. how would i get that to work on mine?
Three answers:
warpjedi
2010-01-26 16:39:54 UTC
What the other dude said...plus your for loops are super confusing.



Usually in the conditional part the looping variable is first:



for (i = 0; i < x; i++)



Just easier to read.



There is also no reason to declare x = 10 since the size is being sent in as a parameter (and the size of your arrays is 9 not 10)



for (i = 0; i < size; i++)
dolchio
2010-01-26 15:48:01 UTC
An overloaded function is one that has the exact same name and return type as another function but has different numbers and/or types of parameters. The appropriate version of the function is called based on what parameters you give it, so in your function definitions you should rename them all to just print.

By the way, it's considered bad practice to initialise a parameter in function brackets, you should initialise it outside the brackets in the main program then pass it in.



EDIT: Write "int size =10;" just before your forward declarations of your functions i.e.



void print(const int data[], int size = 10);

void print(const float data[], int size = 10);

void print(const char data[], int size = 10);



then you can change them to:



void print(const int data[], int size);

void print(const float data[], int size);

void print(const char data[], int size) ;



doing the same in your function definitions as well.



Another tip : if you move the function definitions so that they come before main(), you can get rid of the forward declarations. Some people like having main() first though, so if you're one of those people then it doesn't really matter.
chardip
2016-11-08 07:00:43 UTC
you do no longer call an array. An array 865ccb4ab0e063e5caa3387c1a8741s a var865ccb4ab0e063e5caa3387c1a8741able and not a funct865ccb4ab0e063e5caa3387c1a8741on. you won't be able to do what you like w865ccb4ab0e063e5caa3387c1a8741th a sw865ccb4ab0e063e5caa3387c1a8741tch fact. you would be extra suitable to apply a for loop. for(865ccb4ab0e063e5caa3387c1a8741nt i=0; i < entire; i++) { gadget.out.pr865ccb4ab0e063e5caa3387c1a8741ntln(letters[i]); } have relaxing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...