can someone teach me how to do arrays in C?
i just need the basic idea of the syntax for arrays
it depends on what type of arrays you are looking for: int array you can have something along the lines : int a[size]; or int a[] ={1,2,3,4,5,6,7...} or you could have a dynamical array ( use of pointers) int*a=new int[size]
is it possible to have an array with no limits?
it depends. for an array of which you don't know the size at compilation, you should use the dynamical array(created with pointers). in c++ you must have a fixed sized array. for "infinite" size you could opt to use python and do something like: a=list() and a.append(item) and then you could just use this syntax to do your bidding. but for c/++ you must declare either a static size int a[10] for instance, or int *a=new int[size_you_want]
well then nevermind that. suppose i have a definite size. how should i introduce the array?
what array do you need? int float double char?
it might be interesting to know a summary what those are
what do you want to do with the array? store numbers? letters?
well i'm still just practicing so my idea is that the program asks the user how many numbers he wants to input and then the program asks the user for those n numbers
i tried this main() { int a[100]; int b, c, d; printf("How many numbers do you want? "); scanf("%d", &c); for (b=1; b<=c; b++) { printf("\nEnter number %d: ", b); scanf("%d", d); }
#include <iostream> using namespace std; void askNumbers(){ int a; cout<<"How many numbers do you want to enter?"<<endl; cin>>a; float *b=new float[a]; for(int i(0);i<a;++i){ cout<<"Enter "<<i<<" number"<<endl; cin>>b[i]; } for(int l(0);l<a;++l) cout<<b[l]<<" "; delete []b; } int main() { askNumbers(); return 0; }
just an exemple
that looks like c++ codes?
are they?
well yes, but you can replace cout by stdout or printf it's the same thing
im not really used to those codes...
Join our real-time social learning platform and learn together with your friends!