0

Q: Write a C program to accept information about 4 cars. We consider the program with two-dimensional array “database”, where each row is an individual car, while columns represent parameters of a vehicle

Main function should perform the following tasks.

  • Read the type of vehicle, engine size, mileage, year, and price from the user and stores it in 2 dimensional “database” array.

  • Prints list of cars from “database” with ID, year and price as follows

     ID#    Year    Price
      1     2005    130000
      2     2002    140000
      3     2004    150000
      4     2001    160000
    

#include<stdio.h>

int main() {

    int i, j, count = 0;
    char database[4][5];    
    printf("Please enter body type, engine size, mileage, year, and price of car # %d\n", count++);
    for(i=0; i<4; i++) {
        for(j=4;j<5;j++) {
            scanf("%s", i+1, &database[i][j]);
        }
    }

    printf("\n<<<<<<\nList of cars available on a lot>>>>>>\n");
    printf("\nID#\t\tYear\t\tPrice\n");
    for(i=0; i<4; i++)  {
        for(j=4;j<5;j++) {
            printf("%d\t\t%s\t\t\n", i+1, &database[i][j]);
        }
    }

    return 0;
}

Not working

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • In C or C++? The difference is stark. – tadman Jan 21 '21 at 18:10
  • Just in C language – Muhammad Farhan Jan 21 '21 at 18:11
  • Tip: Declare your iterator variables inside your `for` loop to reduce clutter and help us understand your code more quickly. `for (int i = 0 ; ...)` is the convention to use here. – tadman Jan 21 '21 at 18:11
  • 1
    Does this answer your question? [User input in 2D array (C++)](https://stackoverflow.com/questions/50028478/user-input-in-2d-array-c) – πάντα ῥεῖ Jan 21 '21 at 18:12
  • 1
    `char database[4][5];` can only hold 4 4-character strings. Is that what you want? You need to turn on more compiler warnings here because that `scanf` call looks really wrong. – tadman Jan 21 '21 at 18:13
  • @πάνταῥεῖ No Bro that was in C++ but i need solution in C language – Muhammad Farhan Jan 21 '21 at 18:15
  • @tadman then what should I do here bro :( – Muhammad Farhan Jan 21 '21 at 18:16
  • I'd start by defining a `struct` to represent the data you need to import, then assess the maximum length of the fields you want to process. Using a well-defined `struct` with properties for each of the things you need to track makes your code easy to understand and work with. – tadman Jan 21 '21 at 18:17
  • @πάνταῥεῖ I'm really sorry, please never mind, I'm new here so....... – Muhammad Farhan Jan 21 '21 at 18:19
  • @tadman I do not understand dear what you suggesting ... :( – Muhammad Farhan Jan 21 '21 at 18:22
  • Based on your example, I'd expect to see `struct car { int id; int year; int price; }` as a basis for a solution. You can use `scanf("%d %d %d", &cars[0].id, &cars[0].year, &cars[0].price)` as a way of reading data into a single `car` given an array `struct car cars[N]`. Replace `0` with `i` to make it more generic. – tadman Jan 21 '21 at 18:23
  • @tadman but requirements says only do with 2D array and that's the problem :( – Muhammad Farhan Jan 21 '21 at 18:26
  • Then maybe pivot to `int cars[N][3]` where `N` is how many cars you need and `3` represents the id/year/price triplet. Remember `char` can only store 0..255 or -127..128 depending on your compiler's definition of `char`. – tadman Jan 21 '21 at 18:27
  • @tadman no dear – Muhammad Farhan Jan 21 '21 at 18:29
  • That is a 2D array if that's what you need. `char` is too limited to store the values you want unless you store them as C strings, which would be odd as you'd need a `char [N][3][MAX_LEN]` type array which is 3D. – tadman Jan 21 '21 at 18:34
  • @tadman char [N][3][MAX_LEN] tell me this method – Muhammad Farhan Jan 21 '21 at 18:49

0 Answers0