473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading inputs to vectors

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:
Expand|Select|Wrap|Line Numbers
  1.     while( scanf("%f",&data) != EOF){            
  2.         index++;  
  3.         if(data != 0){
  4.             if(last_x == NULL) {     
  5.                  last_x = (eltype *) malloc(sizeof(eltype));        
  6.                  x->elements = last_x;
  7.                  last_x->data = data;
  8.                  last_x->index = index;
  9.                  last_x->elements = NULL;                 
  10.             }
  11.             else{
  12.                 last_x->elements = (eltype *) malloc(sizeof(eltype));
  13.                 last_x = last_x->elements;
  14.                 last_x->data = data;
  15.                                 last_x->index = index;
  16.                                 last_x->elements = NULL;
  17.                                             }}}
  18.  
printing the inputs
Expand|Select|Wrap|Line Numbers
  1. void printVector (struct svector *x){
  2.     eltype * current_x = x-> elements;
  3. int i;
  4.     for(i=1; i <= x->size; i++){
  5.         if(current_x == NULL || current_x->index > i)
  6.             printf("0 ");
  7.         else {
  8.             printf("%f ", current_x->data);
  9.             current_x = current_x->elements;
  10.         }
  11.     }
  12.     printf("\n");
  13.     scanf("what up");    
  14. }
  15.  
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 "listContainer" 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.
Nov 24 '10 #1
1 1227
Banfa
9,065 Expert Mod 8TB
Using the word vector is slightly confusing since a std::vector is a standard data type in C++.

However you have clearly already implemented a linked list of eltype all you need to do is expand this implementation so that you not only point to the next element in the list, last_x->elements, but you also have an additional pointer to the head of the next list.

Or keep the current list exactly as is and implement a new head list that has pointers to both the head of the next list and the first element of this list.

Generally in pure C casting the output of malloc is considered to be bad practice. If you need to do it that suggests you are actually compiling as C++ not C and if you are using C++ you may as well use std::vector or std::list and save yourself the hassle of implementing your own lists.

Generally it is considered bad practice to not check the return value of malloc and make sure it has not returned NULL.
Nov 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
8
by: mahurshi | last post by:
i have a simple question guys i have to read a file that looks more or less like this AND 1 2 3 OR 2 3 4 INV 5 2 BUF 7 1 AND 1 2
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
19
by: Protoman | last post by:
Hi!!! I need to write a program in which the user defines how many params a mean calculating function uses; here's what I have #include <iostream> #include <cstdlib> #include <vector> #include...
2
by: mahurshi | last post by:
I am trying to read a file full of numbers followed by spaces (and then do some cool stuff with it) My input file looks like this 1 0 1 0 1 0 1 1 1 0 0 1 1 0
1
by: Suma | last post by:
This is more or less a follow up of the previous question I posed yday :) I have vectors V1 and V2 and a functor (less_than) for sorting the vectors. The vectors hold object pointers...
1
by: pallav | last post by:
I am writing a logic simulator for boolean circuits. I have a textfile which will contain vectors like this abc 001 100 111 ... I want to be able to bit pack these vectors to get a = 110, b...
10
by: bejiz | last post by:
Hello, I would like to make a vector which can store vectors within. It is for finding the permutations of some numbers. I thought it would be easy to write some line of code to do this, but...
10
by: sagitalk | last post by:
Here is my code: #include <iostream> #include <vector> #include <fstream> #include <stdio.h> #include <string> #include <cstring> using namespace std;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.