Thread: Sparse Matrix

  1. #1
    Registered User
    Join Date
    Dec 2008

    Sparse Matrix

    Hello all,

    I am new to C programming.

    Need to create a sparse matrix of cars and dealers.

    I created a struct of a car, and an array of cars that holds these cars (say, after production).

    Also I created a dealer struct and array of dealers to hold the dealers.

    Problem #1
    My issue is that although cars exist on their own (in the array of cars), I need to add these cars to dealers within the dealer array.

    I should then be able to run a report on all cars that a dealer has by stepping through the dealer array and looking at each dealer's cars.

    Problem #2
    I need to run a report each car manufactured to see what dealer they are at. This will require stepping through cars in the car array and finding out what dealer they are at. SO there needs to be some type of "active" link between the car and dealer, and I need to be able to trace "backwards" to find out where the car ended up.

    Note for above, per my project specs: I am not allowed to duplicate records, that is, I cannot maintain BOTH a list of dealer records for each car, and a list of car records for each dealer (which is why I chose to create only a list of cars for each dealer)...
    Problem #4

    Obviously this is not a sparse matrix yet.. But am I on the correct path storage-wise? Any hints?

    #include <cstdlib>
    #include <iostream>
    #include <unistd.h>
    #include <stdio.h>
    struct car{
      char * make[39]; // 
      int id;        
    struct car cars[] = {};
    struct dealer{      
      char * name[];
      int id;
      // struct car cars[] = {}; ???????????
    struct dealer dealers[] = {};
    int main (int argc, char *argv[])
      car myCar;
      *myCar.make = "GMC"; = 1;
       cars[0] = myCar;
      dealer aDealer;
      * = "Smith's GMC"; = 201;
     // add cars to the dealer 
        return EXIT_SUCCESS;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    So you're going to have to decide whether you want an array of cars as you have or some sort of sparse matrix-type-thing (I have no idea what you think you mean by that, since a matrix would require two dimensions and you only have one -- the dealership the car is sitting at).

    You will need more fields in your struct, of course; which ones depending on what you end up doing (either a link back to the dealer, or perhaps part of a thread of all the cars at that particular dealer, or something else).

  3. #3
    Registered User
    Join Date
    Sep 2006
    I would keep the records of the cars, and make the dealer array, into just a field in the record of the cars:

    Make: Chevrolet
    Model: Corvette
    VIN: nABnnnnnABCnnnn
    Year: 2008
    Color: Red
    Dealer: Courtesy Chevrolet


    You might think that with thousands of cars, it would take too much work to then get all the cars out in a report on cars for only Courtesy Chevrolet for example, but that's not the case.

    So, car records I would keep, and add another field to it for the dealer, etc. Dealer records I would throw away, after getting that info into the new dealer field.

    Now ALL your reports will be generated only from the Car records (whether in an array, on the HD in a file, etc.).

    Why do you need a sparse matrix?
    Last edited by Adak; 01-02-2009 at 12:52 PM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Quote Originally Posted by brb9412 View Post
    Obviously this is not a sparse matrix yet.. But am I on the correct path storage-wise? Any hints?
    You're on the path to a workable solution to "Problem #2", but it doesn't at all resemble a sparse matrix, so if the main point of the exercise is to implement a sparse matrix, take a look at

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Detecting symmetry in sparse matrix
    By strakerc in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 12:52 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM