Basic Programming help

Ohzopants

My mom thinks I'm cool
Joined
Jan 17, 2004
Messages
165
#1
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

Mainframeguy

Debiant by way of Ubuntu
Joined
Aug 29, 2002
Messages
3,763
#2
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
 

sboulema

OSNN Veteran Addict
Joined
Jun 19, 2002
Messages
2,846
#3
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.
 

X-Istence

*
Political User
Joined
Dec 5, 2001
Messages
6,498
#4
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?
 

Geffy

OSNN Veteran Addict
Joined
Mar 18, 2002
Messages
7,805
#6
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
 
P

PseudoKiller

Guest
#7
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
 

Geffy

OSNN Veteran Addict
Joined
Mar 18, 2002
Messages
7,805
#8
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
 

X-Istence

*
Political User
Joined
Dec 5, 2001
Messages
6,498
#10
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
 

X-Istence

*
Political User
Joined
Dec 5, 2001
Messages
6,498
#11
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.
 
Joined
Jan 6, 2004
Messages
107
#12
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)
 

X-Istence

*
Political User
Joined
Dec 5, 2001
Messages
6,498
#13
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.
 
Joined
Mar 25, 2004
Messages
127
#14
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

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
62,000
Messages
673,421
Members
89,024
Latest member
swipk8