Hello i am new to C programing and i have a problem that should be very easy to deal with.
I need to make a program that read number from the command line and stores them in to a vector and prints them after. Furthermore when ever it reads a 0 as an input it needs to omit it. So far i have done this by:
Reading the inputs:
printing the inputs
And this works fine. But i need when the user presses ENTER meaning anew line to store it in a different vector and keep a reference to it. I program with Java and that can be done easily by having a list "listContai ner" that can store lists inside. And once reading a newline command to create a new list and store it in the listContainer. Finlay i can print each list separately by traversing the listContainer and printing each item in the lists it contains. The problem is i don't know how can this be done in C or if it is possible to do it. Can someone help me please.
I need to make a program that read number from the command line and stores them in to a vector and prints them after. Furthermore when ever it reads a 0 as an input it needs to omit it. So far i have done this by:
Reading the inputs:
Code:
while( scanf("%f",&data) != EOF){
index++;
if(data != 0){
if(last_x == NULL) {
last_x = (eltype *) malloc(sizeof(eltype));
x->elements = last_x;
last_x->data = data;
last_x->index = index;
last_x->elements = NULL;
}
else{
last_x->elements = (eltype *) malloc(sizeof(eltype));
last_x = last_x->elements;
last_x->data = data;
last_x->index = index;
last_x->elements = NULL;
}}}
Code:
void printVector (struct svector *x){
eltype * current_x = x-> elements;
int i;
for(i=1; i <= x->size; i++){
if(current_x == NULL || current_x->index > i)
printf("0 ");
else {
printf("%f ", current_x->data);
current_x = current_x->elements;
}
}
printf("\n");
scanf("what up");
}
Comment