How would someone go about creating an empty array that would be filled from user input ???
Thanks...i have no code for this yet cause i have no clue how to start an empty array
thanks
devilsknight
How would someone go about creating an empty array that would be filled from user input ???
Thanks...i have no code for this yet cause i have no clue how to start an empty array
thanks
devilsknight
You don't have to have it empty. Just fill it up and keep track of your last entry (assuming you aren't using the entire thing). There is no "empty" state in C. You could specify a value as "empty" and check that spot to see if it equals "empty", but there is no such thing as an empty array.
Quzah.
Hope is the first step on the road to disappointment.
what i meant was if i didn't know what the user was to enter, for example if i asked for integer numbers...and he entered 20 how would i tell the array to be that big ?
thanks
Use a vector of strings like this:Code:std::vector<std::string> myStrings; std::string tmp; do { std::getline(std::cin, tmp, '\n'); myStrings.push_back(tmp); } while(tmp != "stop") myStrings.pop_back();
Well, since the STL didn't make it into the C standard, you can use malloc().
Code:int size = 20; int *a = malloc(size * sizeof(int));
God, sorry. I forgot I was in the C forum. Nevermind my post.
> and he entered 20 how would i tell the array to be that big ?
There's no such thing as an expanding array in C.
Either you have
Or you could dynamically extend an allocated pointer by using realloc.Code:int arr[100]; int numEntries = 0; printf( "Enter up to 100 numbers\n" ); for ( i = 0 ; i < 100 ; i++ ) { if ( scanf("%d",&arr[i]) != 1 ) { // not the best input routine // end of data (somehow) numEntries = i; break; } }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Linked list?
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
The closest you can get is realloc.
Quzah.
Hope is the first step on the road to disappointment.
That stuff sounds kind of hard and probably way ahead of what i'll learn in this class
here's the teachers question...the rest of the question i left out cause i pretty much know how to program that part
"In statistics, the mode of a set of values is the value which occurs most often or with the greatest frequency. Write a function that accepts the following arguments:
• An array of integers
• An integer that indicates the number of elements in the array
"
how would i go about programming that ? if we didn't know how many numbers a user were to say enter then having to store them into an array ?
or should i make the array rediculously large lol
Just declare an array (of some size).
Then ask the user "How many numbers do you want to input?"
If that's bigger than your array size, just print a message and exit.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If the number of elements is input first, you don't need the realloc() stuff. You can go with simple malloc(). As long as you've learned pointers, that's enough.
If you haven't learned pointers, either go with Salem's approach or complain to the teacher.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
ok i think i figured out a way around it...i ask the user for how many numbers it wants to enter...then pass that number to a function and create an array
however i get this error:
constant expression required in function meanArray
Code:#include <stdio.h> /* In statistics, the mode of a set of values is the value which occurs most often or with the greatest frequency. Write a function that accepts the following arguments: • An array of integers • An integer that indicates the number of elements in the array */ int meanArray(int arrayNum); int main() { int num; printf("How many numbers do you want to enter ? "); scanf("%i", &num); meanArray(num); return 0; } int meanArray(int arrayNum) { int makeArray[arrayNum]; return 0; }
Last edited by devilsknight; 07-18-2006 at 02:46 PM.
Woudn't it be easier just to have an array already declared of a fixed length? i.e. can you make assumptions about the maximum number of elements the user will input?
Is there a requirement that the array has to be dynamically created based on user input?
Just trying to make your job easier...
yah the project states that i must let the user input what size then pass it along...
so how do i fix that error ???
i'm been workin away on it and have no clue how to work it