|
'C' inquiry > arrays
|
View this Thread in Original format
| digitalbreach |
kinda have a question for all u programming ppl.
having trouble reading in this info :
3 4
0011
0110
1010
able to read in the first two into separate variables. Trying to read in the first line('0011') of matrix and then add the second and finally third as one single array. in this case w/ 12 elements thus an >> int variable w/ 12 cells.
how would I go about this.
thanX |
|
|
| milanster |
are u entering these as arguments into the prog?
if so...
then just make an array of type int with the same size as the number of args
then do the following loop
int temp; // a temp int to store the current argument
int arr[#ofargs]; // a new array having the size of args
for (int i =0; i<#ofargs; i++)
{
sscanf(argv[i],"%d",&temp); // stores each arg in "temp"
arr[i] = temp; //stores the current value
//of temp in the array
}
hope this helps! |
|
|
| AnotherWay83 |
from ur post it looks like u're typing in sumthing like this at the command prompt (assume the program is called 'matrix', and data.txt is the file containing the information)
matrix < data.txt
and the information in data.txt is formatted like so :
//first line of file
thats the first line of the text file, and then, using the information u now have about the matrix (after scanning those in), u can set up a loop statement (perhaps even a nested loop) to get all the elements of the matrix...but instead of a multidimensional array, u wish to scan them all consecutively into a one dimensional array, so that, in effect, a data file in which the information is like this:
2 2
01
10
will end up giving u a one-dimensional matrix, with size 4, (lets call it 'matr') whose elements are: 0,1,1,0
in that order.
here's how i would go abt it:
#include
int *arr; //will hold the elements of the matrix
main() {
int a,b; //these hold dimensions of matrix
scanf(" %d",&a); //note the space, it is intentional
scanf(" %d",&b);
a*=b; //total number of elements
//allocate space
arr=(int *) malloc((a+1)*sizeof(int)); //(a+1) to put in '\0' at end
b=0;
while(b
scanf(" %1d",arr+b++); //each element is only 1 digit long
}
*(arr+a)='\0';
b=0;
while (b
printf("%d ",*(arr+b++));
}
}//end main
that program will work only for single digit entries...i havent built in any error trapping into it, tho
to use it, create a data file (in the same folder as the exe of the program), and if ur program is named 'matrix', type in this at the command prompt:
matrix < data.txt
in place of data.txt put the name of ur data file
hth |
|
|
| sothis |
he probably would have learned way better (and retained it a lot more) if you guys would have just explained the concept, and not done his program FOR him.
always irks me when people post their programming assignments/problems in here and people do their for them. that isnt how you learn.
(note: its different when the person posts their own code, and you point out whats wrong with it). |
|
|
| bassaholix |
| quote: | | he probably would have learned way better (and retained it a lot more) if you guys would have just explained the concept, and not done his program FOR him. |
HERE! HERE! |
|
|
| digitalbreach |
actually i'm more of visual :eyespop: person. I wish that would have been all of it.
anyways...to Anotherway83
what do the following lines mean
arr=(int *) malloc((a+1)*sizeof(int)); //(a+1) to put in '\0' at end
never seen "malloc" before |
|
|
| sothis |
you are programming in C, and you havent seen malloc?
ummmmm |
|
|
| Orbax |
| take your shoes off and roll in some poop and then run around naked and pee somewhere. I dont know how much itll get done in C, but youll feel better |
|
|
| AnotherWay83 |
| quote: | Originally posted by digitalbreach
actually i'm more of visual :eyespop: person. I wish that would have been all of it.
anyways...to Anotherway83
what do the following lines mean
arr=(int *) malloc((a+1)*sizeof(int)); //(a+1) to put in '\0' at end
never seen "malloc" before |
usually when u declare an array, u do sumthing like
int arr[3];
so the compiler generates code that automatically sets aside the space equivalent to 3*sizeof(int) (i hope u know abt the sizeof operator :D)...(well its not really 'code' but for now lets leave that aside)
with malloc u dont have to set aside the space in advance...so in effect the size of ur .exe will be smaller
malloc makes a request to the OS for the amount of space u need, and does this at runtime basically...ur C book should've covered this, and prolly in more detail :D
hth |
|
|
|
|