Sparse Matrix

This is a discussion on Sparse Matrix within the C Programming forums, part of the General Programming Boards category; Hello all, I am new to C programming. Need to create a sparse matrix of cars and dealers. I created ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    4

    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?

    Code:
    #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";
       myCar.id = 1;
       cars[0] = myCar;
     
      dealer aDealer;
      *aDealer.name = "Smith's GMC";
       aDealer.id = 201;
     // add cars to the dealer 
     
     
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Posts
    8,868
    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

    etc.

    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
    Posts
    595
    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 http://en.wikipedia.org/wiki/Sparse_matrix

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, 02:30 PM
  2. Detecting symmetry in sparse matrix
    By strakerc in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01: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 Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21