Basic Programming help

Ohzopants

My mom thinks I'm cool
Joined
17 Jan 2004
Messages
165
Hey guys...

I have this assignment due next week and I have little to no programming experience. My major problem is: How do i create an array whose size depends on a number of user inputs?

I can't post any of the code I've tried because most of it won't compile and whatever does just produces junk.

Please help guys...
 

Attachments

  • Ass_6.pdf
    13.3 KB · Views: 116
Make your subscripts variables - you'll need to check the parameters users input for limits of your platform/language too - and your language will have to support this feature or you're going to have to do something more complex as a workaround which I haven't time to go into here.

If this makes no sense to you then you're just gonna have to bite the bullet and study a bit :p
 
only know how to do this in visual basic.

dim variable()

collect user inputs in temp variables
once you know the final number of inputs

redim variable(number inputs)
transfer to the temp variables to the array

something like that.
 
Ohzopants said:
Hey guys...

I have this assignment due next week and I have little to no programming experience. My major problem is: How do i create an array whose size depends on a number of user inputs?

I can't post any of the code I've tried because most of it won't compile and whatever does just produces junk.

Please help guys...

What language?
 
coulda helped no problem were it java, though basically you just try catch and if you try to write to an array index which is out of bounds you catch it and then double or increase the array size, by making a copy of the working array but with a few more spaces, copy the data from the working array into the new larger array and then start working with the new array
 
I am going to give Geffy all my Java homework and see if he can do it :D as a test... yeah as a test, just to see if he's as good as he says
 
feck off, I got Class Diagrams to do for sheep as coursework for Internet Computing Fundamentals, a SMIL Presentation of my department for Creative Media Technology and a Library System to write in Java for Object Oriented Programming and Design
 
hsn said:
its C++, its in the attahcment.

I just read the attachment, had no Adobe or PDF reader on the other PC i was on.

And actually i has to be in C. If it were in C++, it would be easy.

You would be able to use:

std::vector, this is basically an array that can be increased as much as you want


For C, youd need to know how to use dynamic memory allocation schemes, and how to free it. It is quite a bit harder.

Here is some sample C++ code on how it would be done in C++. I am not a good C programmer, so i cant write you a program in C.

Code:
#include <iostream>
#include <cstdlib>
#include <vector>

using std::cin;
using std::cout;

int main()
{
        std::vector<float> fltVector;
        std::string input("x");

        cout << "Usage: \n\tKeep entering a number, this includes decimals, when you have entered all the numbers, type an \"x\"\n\n(c) Copyright X-Istence.com\n\n";

        // We run the while loop forever
        while (1)
        {
                // Get our first input.
                cout << "Please enter a number: ";
                cin >> input;

                // If the input is empty, we end this loop, and go on to the rest of the program
                if (input == "x" || input == "X")
                        break;

                // Add the number to the float vector.
                fltVector.push_back(atof(input.c_str()));
        }

        // User decided they have inputted enough info

        // We need an iterator to be able to go over the entire vector
        std::vector<float>::iterator iter;

        // Create another float to contain the total, so we can get the mean out of it.
        // I have no clue what variance/deviation is, so ill just keep it to the mean.
        float total = 0;

        for (iter = fltVector.begin(); iter != fltVector.end(); iter++)
                // Add the amount that the vector has to total.
                total += *iter;

        // A bit of output can not hurt us
        cout << "\n\n\nYou have entered " << fltVector.size() << " items.";
        cout << "\n\nThe mean of the numbers you have entered is " << total / fltVector.size() << "\n";

return 0;
}

Example:

The mean of the numbers you have entered is 4.96064e+11
bash-2.05b$ ./a.out
Usage:
Keep entering a number, this includes decimals, when you have entered all the numbers, type an "x"

(c) Copyright X-Istence.com

Please enter a number: 50
Please enter a number: 60
Please enter a number: 70
Please enter a number: x



You have entered 3 items.

The mean of the numbers you have entered is 60
bash-2.05b$ ./a.out
Usage:
Keep entering a number, this includes decimals, when you have entered all the numbers, type an "x"

(c) Copyright X-Istence.com

Please enter a number: 0.0000005
Please enter a number: 0.0000006
Please enter a number: 0.0000007
Please enter a number: x



You have entered 3 items.

The mean of the numbers you have entered is 6e-07
 
I do not know the limits of the type "float", and i do know that it should not be used all that often as it is really not that portable.

There is thus a possibility of a buffer overflow.
 
X-Istence said:
I do not know the limits of the type "float", and i do know that it should not be used all that often as it is really not that portable.

There is thus a possibility of a buffer overflow.

you can use the sizeof() function to see the limit of float, or you can just go with a double which is set to 64 bits on x86 cpu's (I think, I may be wrong)
 
CHiLLaXen said:
you can use the sizeof() function to see the limit of float, or you can just go with a double which is set to 64 bits on x86 cpu's (I think, I may be wrong)

sizeof() will show different results for different OS's on a float, which is why its not really all that portable.

The document stated that i had to use float, otherwise i would have indeed just rather gone with double.
 
I know the assignment is over, but for something as simple as this I'm supprised everyone overcomplicated it. Create a function called array_increment. This is going to be process intensive, but since the user is manually entering in values one at a time it isn't that big of a deal. This function just increases an array size by one each time it is called.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back