7 Replies - 909 Views - Last Post: 18 April 2010 - 09:14 AM Rate Topic: -----

#1 keeper962   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Having problem outputting sets of data

Posted 18 April 2010 - 08:10 AM

I am having a problem with this program:

Write a program that accepts from keyboard integer value which stands for the # of data sets of random
integer each in the range [5, 10] and outputs all data sets in the elements, and the set of data that results in the larger sum.

Here is the code I have so far not sure what to add but I know I am missing something.
#include <iostream>
#include <cmath>
#include <iomanip>
double random (unsigned int & seed);
void print_data_set(int a[]);
int sum_of_data(int a[]);
void copy_sets(int a[], int b[]);
void set_up_data(int a[]);
const int SIZE=10;
int a[SIZE], maxarray[SIZE];
int i,j,k;
int  sum, maxsum;
unsigned int seed;

using namespace std;

int main()
{
    cout<<"Enter Number of Sets You Want: ";
    cin>>i;

    for (i=1; i<k; ++i)
    {
    set_up_data(a);
    sum=sum_of_data(a);
    
    if (sum>maxsum)
        maxsum=sum;
    }
    return maxsum;
 
}

void set_up_data(int a[])
{
    for (int i=0; i<SIZE; ++i)

    a[i]=int (5+6*random(seed));
}

int sum_of_data(int a[])
{
    for (j=1; j<10; ++i)
    {
    a[j]=int (5+6*random(seed));
    sum =sum+ a[j];
    }
    return sum;
}           


double random (unsigned int & seed)
{
    const int MODULUS =15749;
    const int MULTIPLIER=69069;
    const int INCREMENT=1;
    seed=((MULTIPLIER*seed)+ INCREMENT)% MODULUS;   
    return double (seed)/double (MODULUS);
}

void copy_sets(int a[], int b[])
{
    for (int i=0; i<SIZE; ++i)
    a[i]=b[i];
}

void print_data_set(int a[])
{
      for (i=1; i<SIZE; ++i)
    {
    cout<<setw(3)<<a[i];
    if (i%10==0)
    
        cout<<i<<endl;
    }
}



Thank You for Helping.

Is This A Good Question/Topic? 0
  • +

Replies To: Having problem outputting sets of data

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Having problem outputting sets of data

Posted 18 April 2010 - 08:20 AM

You haven't said what's giving you trouble.

Not sure if this is an issue, but I'd probably do this:
    a[i]=5+ rand() % 6;


where you have this:
    a[i]=int (5+6*random(seed));


and I'd set the random seed in an initialization function.
Was This Post Helpful? 0
  • +
  • -

#3 keeper962   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Re: Having problem outputting sets of data

Posted 18 April 2010 - 08:23 AM

Sorry
The problem I am having is when it asks how many sets I want I type in a number but nothing happens
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Having problem outputting sets of data

Posted 18 April 2010 - 08:32 AM

for (i = 1; i < k; ++i)


k is 0. Of course there aren't any iterations.

This post has been edited by sarmanu: 18 April 2010 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Having problem outputting sets of data

Posted 18 April 2010 - 08:33 AM

Well, look at this:
    cin>>i;

    for (i=1; i<k; ++i)


It says take the input and put it in the variable i, then start a for loop where you reset i to 1, check to see if it is less than k (which is probably 0 because you haven't set it) and if it's not, which it probably isn't, end the loop!
Was This Post Helpful? 0
  • +
  • -

#6 keeper962   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Re: Having problem outputting sets of data

Posted 18 April 2010 - 08:54 AM

Sorry I am a little confused I am not sure what needs fixing can anyone show me?
Was This Post Helpful? 0
  • +
  • -

#7 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Having problem outputting sets of data

Posted 18 April 2010 - 08:56 AM

 for (i=1; i<k; ++i)


k is 0, so that loop becomes:
 for (i=1; i<0; ++i)


which does not make sense. You have to find a way to loop from 1 -> i (or 0 -> i - 1). If you have written that program, you should know how to do that.
Was This Post Helpful? 0
  • +
  • -

#8 keeper962   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 23-February 10

Re: Having problem outputting sets of data

Posted 18 April 2010 - 09:14 AM

View Postsarmanu, on 18 April 2010 - 07:56 AM, said:

 for (i=1; i<k; ++i)


k is 0, so that loop becomes:
 for (i=1; i<0; ++i)


which does not make sense. You have to find a way to loop from 1 -> i (or 0 -> i - 1). If you have written that program, you should know how to do that.



Wouldn't k be what ever I input when asked so say I type in 5 k becomes 5 not 0.
I probably have it wrong but thats what I thought ; if that is wrong do I need have k =1 before the loop starts but that doesn't make sense to me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1