473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Array-related Functions

59
i have a function called showArray(int ar[], int numElements, and int numPerLine)
i am having some issues with creating the function properly so that the caller can call it. so far i have this:

/******************************
Function Name: void showArray(int ar[], int numElements, int numPerLine)



******************************/
void showArray(int ar[], int numElements, int numPerLine)
{
int i;`


while(numElements < 200)
{
i = ar[0];
cout << i;
i++;
}
if(numPerLine *= 2)
cout << endl;


an array will be passed through which has 200 elements. in main i have a symbolic const for the number of elements but dont know what passes through the other two arguments. any suggestions?!?! i know the function is coded incorrectly also! Thanks
Mar 29 '07 #1
33 2666
brady
59
By the way, the function needs to display the elements of the array, several item per line as specified by the third argument. The seconds argument tells the function how many items are in the array, so it knows where to stop. i need to output a newline character when the number of items slready printed is a multiple of numPerLines. maybe the % operator? Thanks
Mar 29 '07 #2
Savage
1,764 Expert 1GB
Hi brady,

You have not defined a good condition for your while loop.You cannot predict
that your array will have 200 elements.Why not use instead something like:

Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2. int element
  3.  
  4. while(i<numElements)
  5. {
  6.    element=ar[i];
  7.    ar[i-1]='\0';
  8.    cout<<element;
  9.    i++;
  10. }
Hmm,for third argument you could integrate inside loop a counter which will count up to numPerLine and then it must become counter=counter%numPerLine and cout \n character to force the start of a new line.


Savage
Mar 29 '07 #3
brady
59
Hello Savage,
Thanks for the reply! That makes a lot more sense then what i had. For the counter, can i just place that right after incrementing i? then maybe have a decision statement. im new with C++ so its a bit confusing.

Hi brady,

You have not defined a good condition for your while loop.You cannot predict
that your array will have 200 elements.Why not use instead something like:

Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2. int element
  3.  
  4. while(i<numElements)
  5. {
  6.    element=ar[i];
  7.    ar[i-1]='\0';
  8.    cout<<element;
  9.    i++;
  10. }
Hmm,for third argument you could integrate inside loop a counter which will count up to numPerLine and then it must become counter=counter%numPerLine and cout \n character to force the start of a new line.


Savage
Mar 29 '07 #4
Savage
1,764 Expert 1GB
Hello Savage,
Thanks for the reply! That makes a lot more sense then what i had. For the counter, can i just place that right after incrementing i? then maybe have a decision statement. im new with C++ so its a bit confusing.

Yes,that should do it!!!

Savage
Mar 29 '07 #5
brady
59
Another question, is the '\0' a null value?

Hi brady,

You have not defined a good condition for your while loop.You cannot predict
that your array will have 200 elements.Why not use instead something like:

Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2. int element
  3.  
  4. while(i<numElements)
  5. {
  6.    element=ar[i];
  7.    ar[i-1]='\0';
  8.    cout<<element;
  9.    i++;
  10. }
Hmm,for third argument you could integrate inside loop a counter which will count up to numPerLine and then it must become counter=counter%numPerLine and cout \n character to force the start of a new line.


Savage
Mar 29 '07 #6
Savage
1,764 Expert 1GB
No,it's a terminating character which will ensure that you don't acsidently
cout previos element.


Savage
Mar 29 '07 #7
brady
59
Yes thats right! Thanks for the refresher and thanks for your help! This function was driving me crazy! i'll post the final here in a minute and if you're around and happen to view the post and see a mistake could you let me know? i cant test it yet because im having issues passing arguments into the function. im getting the invalid conversion to int*, int or something like that

No,it's a terminating character which will ensure that you don't acsidently
cout previos element.


Savage
Mar 29 '07 #8
Savage
1,764 Expert 1GB
Yes thats right! Thanks for the refresher and thanks for your help! This function was driving me crazy! i'll post the final here in a minute and if you're around and happen to view the post and see a mistake could you let me know? i cant test it yet because im having issues passing arguments into the function. im getting the invalid conversion to int*, int or something like that
Please post just the troubleshooting part because if you post full code it will
be snipped by the moderators(see posting guidelines)

Savage
Mar 29 '07 #9
brady
59
I didnt know that. and yes i will follow those guidlines. thanks for the fore warning

Please post just the troubleshooting part because if you post full code it will
be snipped by the moderators(see posting guidelines)

Savage
Mar 29 '07 #10
Savage
1,764 Expert 1GB
I didnt know that. and yes i will follow those guidlines. thanks for the fore warning
Always a pleasure..


Savage
Mar 29 '07 #11
brady
59
Well i received a successful build after implementing the counter. just hope it works. here is what i coded after i was incremented:
while....
//body of loop previously posted
counter++;
if(counter = counter%numPerLine)
cout << "/n";

Always a pleasure..


Savage
Mar 29 '07 #12
Savage
1,764 Expert 1GB
Well i received a successful build after implementing the counter. just hope it works. here is what i coded after i was incremented:
while....
//body of loop previously posted
counter++;
if(counter = counter%numPerLine)
cout << "/n";
Here I can find 2 errors.

FIRST:


Expand|Select|Wrap|Line Numbers
  1. if(counter = counter%numPerLine)
is invalid.This condition will never become TRUE.

try using instead

Expand|Select|Wrap|Line Numbers
  1. if(counter==numPerLine) counter=counter%numPerLine,cout <<"\n";
SECOUND:

You might noticed that i used == when declaring a condition.Without these it would submit message:

"Possibly incorrect assigment ........"

Savage
Mar 29 '07 #13
Savage
1,764 Expert 1GB
Sorry for double post but i maked a mistake .The condition might become TRUE but I think that is not the best solution,anyway you would need to
"reset" a counter.


Savage
Mar 29 '07 #14
brady
59
Yea that makes sense because im not assigning values. Can i code it like this:
if(counter==numPerLine)
counter=counter%numPerLine;
cout <<"\n";
is this the same as how you had it on one line?

Here I can find 2 errors.

FIRST:


Expand|Select|Wrap|Line Numbers
  1. if(counter = counter%numPerLine)
is invalid.This condition will never become TRUE.

try using instead

Expand|Select|Wrap|Line Numbers
  1. if(counter==numPerLine) counter=counter%numPerLine,cout <<"\n";
SECOUND:

You might noticed that i used == when declaring a condition.Without these it would submit message:

"Possibly incorrect assigment ........"

Savage
Mar 29 '07 #15
brady
59
Not a prob. i can reset the counter like so, right?


if(counter==numPerLine)
counter=counter%numPerLine;
cout <<"\n";
counter = 0;

Sorry for double post but i maked a mistake .The condition might become TRUE but I think that is not the best solution,anyway you would need to
"reset" a counter.


Savage
Mar 29 '07 #16
Savage
1,764 Expert 1GB
Yea that makes sense because im not assigning values. Can i code it like this:
if(counter==numPerLine)
counter=counter%numPerLine;
cout <<"\n";
is this the same as how you had it on one line?
Yes,it's same.Operator ',' serves to declare actions that are correlated and it makes your code smaller.

Note:Use this operator only when actions are correlated or u might get lost

in code!!!

Savage
Mar 29 '07 #17
brady
59
so instead of having lines of code like i just did it, the ',' operator lets you code, in our example, an entire loop on one line? very interesting. i didnt know that!

Yes,it's same.Operator ',' serves to declare actions that are correlated and it makes your code smaller.

Note:Use this operator only when actions are correlated or uou might get lost

in code!!!

Savage
Mar 29 '07 #18
Savage
1,764 Expert 1GB
Well,no one is borned with knowledge.

We exist to learn.


Savage
Mar 29 '07 #19
brady
59
Another question for you, if you dont mind. my caller for that function is as followed:

showArray(storescores, 50, 8);

storescores is my array to hold scores from a file with 200 spaces available
50 and 8 are numbers i just made up to see if they would pass through


but im receiving a invalid conversion from int* to int. why would that be?
Mar 29 '07 #20
brady
59
ha ha that is true. i like that

Well,no one is borned with knowledge.

We exist to learn.


Savage
Mar 29 '07 #21
Savage
1,764 Expert 1GB
Another question for you, if you dont mind. my caller for that function is as followed:

showArray(storescores, 50, 8);

storescores is my array to hold scores from a file with 200 spaces available
50 and 8 are numbers i just made up to see if they would pass through


but im receiving a invalid conversion from int* to int. why would that be?
Hmm,how have you declared storescores?

Savage
Mar 29 '07 #22
brady
59
Yup, like so:
int storescores[200];

i had a symbolic constant of scores set to 200 and then i had the array like so:
storescores[scores];
but that was also giving me the same error

Hmm,how have you declared storescores?

Savage
Mar 29 '07 #23
Ganon11
3,652 Expert 2GB
Just a message that I've renamed the thread to be a bit more descriptive and not so verbose. :) Continue on!

MODERATOR
Mar 29 '07 #24
brady
59
Thank you moderator, i was panicing and got a little keyboard happy. :)

Just a message that I've renamed the thread to be a bit more descriptive and not so verbose. :) Continue on!

MODERATOR
Mar 29 '07 #25
Savage
1,764 Expert 1GB
Yup, like so:
int storescores[200];

i had a symbolic constant of scores set to 200 and then i had the array like so:
storescores[scores];
but that was also giving me the same error
try declaring function as following:

Expand|Select|Wrap|Line Numbers
  1. void showArray(int *ar[],int numElements,int numPerLine);

Savage
Mar 29 '07 #26
brady
59
That would be before main() right?

try declaring function as following:

Expand|Select|Wrap|Line Numbers
  1. void showArray(int *ar[],int numElements,int numPerLine);

Savage
Mar 29 '07 #27
Savage
1,764 Expert 1GB
That would be before main() right?
Yes,before main() and after it when defining what is function supposed to do.

(Thats,the part u first posted)


Savage
Mar 29 '07 #28
brady
59
Got a successful build. i declared it before main as:
void showArray(int *ar[],int numElements,int numPerLine)
and i also changed the caller of the function to that also. is that correct?

Yes,before main() and after it when defining what is function supposed to do.

(Thats,the part u first posted)


Savage
Mar 29 '07 #29
Savage
1,764 Expert 1GB
Got a successful build. i declared it before main as:
void showArray(int *ar[],int numElements,int numPerLine)
and i also changed the caller of the function to that also. is that correct?

Yes it is.Bravo!!!


Savage
Mar 29 '07 #30
brady
59
Now correct me if im wrong but shouldnt the caller of the function (in main) be something different from what i declared the function as (before main)?

Yes it is.Bravo!!!


Savage
Mar 29 '07 #31
brady
59
Savage,
Thanks for all your help. greatly appreciated!

Now correct me if im wrong but shouldnt the caller of the function (in main) be something different from what i declared the function as (before main)?
Mar 29 '07 #32
Savage
1,764 Expert 1GB
Now correct me if im wrong but shouldnt the caller of the function (in main) be something different from what i declared the function as (before main)?
Just a litlle.The main differance is that when calling a function you don't

type like:

Expand|Select|Wrap|Line Numbers
  1. void showarray(int *ar[],int numOfElements,int numPerLine);

You type it like:

Expand|Select|Wrap|Line Numbers
  1. showarray(scores,200,50);
for e.g


Savage
Mar 30 '07 #33
Savage
1,764 Expert 1GB
Savage,
Thanks for all your help. greatly appreciated!

Just as I said:

Always a pleasure to help...


Savage
Mar 30 '07 #34

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.