Fedora Linux Support Community & Resources Center
  #1  
Old 17th January 2008, 08:42 PM
Greg A Offline
Registered User
 
Join Date: Dec 2007
Posts: 28
arrays problem...please help me identify what is wrong

I'm trying to write a program that will compare the sum of 2 throws of dice (one throw comprised of 5 dice, the other comprised of 6) so far I'm at the stage where I am trying to make a function to compute the sum for throwing 6 dice but am having a nightmare trying to get my function dice_sum() to work correctly...it just assigns to 'a' the value 0, when I am asking it to add to a the value of array[i] (where i is a position in the for-loop)

I far from competent at programming and suspect I have made mistakes where I think my code is correct. Can someone take a look at the code below and throw me some hints as to why I'm not getting what I'm expecting?

PHP Code:
#include <iostream>
#include <ctime>

using namespace std;

int random_One_To_Six();
void dice_Maker();
int dice_Sum(int array[]);
 
int main()
{
srand(time(0));

dice_Maker();


  return 
0;
}

int random_One_To_Six()
//this function will take a random number and convert it to a number from one to six based on its remainder modulo 60.
//Need to define this function better perhaps using switch but it seems to work ok for now.
{
double Random = (rand())%60;
int a 0;

if (
Random >=&& Random <=9)
1;
if (
Random >=10 && Random <=19)
2;
if (
Random >=20 && Random <=29)
3;
if (
Random >=30 && Random <=39)
4;
if (
Random >=40 && Random <=49)
5;
if (
Random >=50 && Random <=59)
6;
return 
a;
}

void dice_Maker()
//This function fills each element of an array of length 6 with a number from 1 to 6 as determined by the random_one_To_Six() function.
{
int length 5;
int array[6];
for(
int i=0;<= lengthi++)
{
array[
i] = random_One_To_Six();
}
// I now want to send this array to a function that adds together all the elements within my array and return its sum.
cout << dice_Sum(array);
}

int dice_Sum(int array[])
{
int a=0;
for(
int i=0<=5i++)
{
a: array[i]+a;//this bit isn't working...I want it to add up the elements from array[0] to array[5]
cout << "**" << << "**" << endl// this bit is to check what 'a' is when I change things and figure out what is up.
}
return 
a;

Reply With Quote
  #2  
Old 17th January 2008, 09:09 PM
wowbag1 Offline
Registered User
 
Join Date: Jan 2008
Posts: 9
In function dice_sum you have a colon instead of an equals sign a: array[i]+a; should be a=array[i]+a;
Try that see how it goes.
Reply With Quote
  #3  
Old 17th January 2008, 09:11 PM
adrianx's Avatar
adrianx Offline
Registered User
 
Join Date: Jul 2007
Location: South Africa
Posts: 953
Quote:
Originally Posted by wowbag1
In function dice_sum you have a colon instead of an equals sign a: array[i]+a; should be a=array[i]+a;
Try that see how it goes.
You beat me to it, I was typing a bit slowly...
__________________
Registered Linux User 460110
Reply With Quote
  #4  
Old 17th January 2008, 10:12 PM
Greg A Offline
Registered User
 
Join Date: Dec 2007
Posts: 28
cheers guys...couldn't see that!
Reply With Quote
  #5  
Old 17th January 2008, 09:10 PM
adrianx's Avatar
adrianx Offline
Registered User
 
Join Date: Jul 2007
Location: South Africa
Posts: 953
I'm not really a C programmer, but shouldn't a: array[i]+a be
Code:
a = a + array[i];
OR
a += array[i];
?
__________________
Registered Linux User 460110
Reply With Quote
Reply

Tags
arrays, identify, problemplease, wrong

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple raid arrays. toys19 Hardware & Laptops 5 22nd January 2009 09:28 AM
PHP arrays... fnord Programming & Packaging 12 5th December 2005 09:59 PM
Arrays in C eggsy85 Programming & Packaging 14 23rd April 2005 05:36 AM
Semaphore Arrays LackeyLad Programming & Packaging 2 5th November 2004 05:51 AM


Current GMT-time: 07:15 (Wednesday, 19-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat