Can anyone make sense of these instructions?

1. Define a struct type SDistance that contains two integer member, feet and inches.

2. Define a function that receives an array of SDistance and how many distances on the array as parameter. The function will ask input for feet and inches for each distance on the array.

3. Define another function that receives an array of SDistance and how many as parameter. The function will display each distance on the array without modifying their data.

4. Define yet another function that receives an array of SDistance and how many as parameter. The function will add all the distances and return the total. The inches value of the returned total should be less than 12. The function should not modify the distances stored on the array while adding them.

5. Write a driver program to test these functions described above.

I'm not asking for the code or anything like that , i just don't understand what i am supposed to be doing. Can anyone explain this easier?
Yep, understood all of that.

Which bit are you stuck with in particular? Have you covered structs?
Yes I've covered structs. I understand the first part.

struct SDistance
{
int feet, inches;
};

2 and 3 don't make much sense to me, this part in particular:

Define a function that receives an array of SDistance and how many distances on the array as parameter
Admittedly, that's pretty badly worded.

Basically, it's looking for a function that takes an array SDistance structs as the first parameter.

The second parameter needs to be an integer containing the size of the array.

1
2
3
4
void SomeFunc( MyStruct *structs, int size )
{
  // Do stuff
}


Note, I've passed the array using the pointer syntax. You can pass it similarly using MyStruct structs[].
#include <iostream>
#include <conio.h>
using namespace std;

struct SDate
{
int mm, dd, yy;
};

void getArrayOfDates(SDate [], int);
void displayArrayOfDates(const SDate [], int);

int main()
{
SDate days[5];
int i;

getArrayOfDates(days, 5);
displayArrayOfDates(days, 5);

_getch();
return 0;
}

void getArrayOfDates(SDate dates[], int size)
{
char ch;

for(int i = 0; i < size; ++i)
{
cout << "Enter a date in the format of mm/dd/yyyy: ";
cin >> dates[i].mm >> ch
>> dates[i].dd >> ch
>> dates[i].yy;
}
}

void displayArrayOfDates(const SDate dates[], int size)
{
cout << "\n\nThe dates are:\n\n";
for(int i = 0; i < size; ++i)
{
cout << dates[i].mm << '/'
<< dates[i].dd << '/'
<< dates[i].yy << endl;
}

cout << endl;
}

This code was an example in her notes. Is this code sort of like the other one?
Yeah, both of those functions have the parameters listed in the original brief (though obviously with a struct named SDate, not SDistance).

Do you see the purpose of passing the size variable? Without it, there'd be no way to tell when to stop looping through the array.
I understand what size is for, but what value does it have?
"size" is a parameter passed to the function, the value it has is given to the function when the function is called, so it can be different for each time the function is called.

In main, it looks like the function is called with size as 5.
Whatever the size of your array is.

1
2
3
4
5
const int ARR_SIZE = 10;
MyStruct structs[ARR_SIZE]; // An array of 10 MyStructs

// Assuming I'm calling my function from a couple of posts ago...
SomeFunc( structs, ARR_SIZE );
So for the problem, I'd just make it what I want since it doesn't ask for a specific size?
Well when you write the function itself you're not going to care about the size. You have the value you need as the argument named "size" and you can use that locally within the function.

You only need to worry about passing in the size value you need when you come to the final part: writing the "driver" code.
Is the "driver" code just the code? I've never heard her use the term driver code until now.
I think iHutch meant "client code" and not "driver code". "client code" is the code that uses the code you are writing. Even if your code and the client code are all in the same program, the distinguishment is still made.
So i've been working on it and it runs up to bullet 3. Why would the total for inches need to be less than 12?

#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

struct SDistance
{
int feet, inches;
};

void getSArray(SDistance [], int);
void displayArray(const SDistance [], int);

int main()
{
SDistance length[5];
int i;

getSArray(length, 5);
displayArray(length, 5);

_getch();
return 0;
}

void getSArray(SDistance distance[], int size)
{
for(int i = 0; i < size; ++i)
{
cout << "Enter feet: ";
cin >> distance[i].feet;

cout << "Enter inches: ";
cin >> distance[i].inches;
}
}

void displayArray(const SDistance distance[], int size)
{
cout << "\n\nThe distances are:\n\n";

for(int i = 0; i < size; ++i)
{
cout << "Feet: " << distance[i].feet << " Inches: " << distance[i].inches << endl;
}

cout << endl;
}

void AddArray(const SDistance distance[], int size)
{
cout << "\n\nAdding them all up gives you this:\n\n";
}
SlenderMan wrote:
Why would the total for inches need to be less than 12?
At 12 inches, you have a foot.
Oh so its going to add all the inches and feet you entered and for every 12 inches its going to turn it into a foot?
Topic archived. No new replies allowed.