Hi, Am very very struct is there possible way to multiple variable into one element in array c
and is there posible to do case statement with array.
What I try to if max capacity of ship is less than max capacity of harbour but I need to check every array of max capacity of harbour which content 10 element if max capacity of ship is less than max capacity of harbour in array address 10 t0 10 then the store to array each of them I actually dont know how to explain in word.

Here is my code :

#include <stdio.h>
#include <string.h>
# define Line_size 200

int sort(const void *x, const void *y) 
{
    return (*(int*)x - *(int*)y);
}
int determindCapacity(int lineCount2, int lineCount, char harbour[Line_size][Line_size], char ship[Line_size][Line_size])
{
    int i, j=0;
    // Harbour array element
    int id[Line_size];
    int maxCap[Line_size];
    int perUnit[Line_size];
    double amount[Line_size];
    char name[Line_size];
    // Ship array Element
    int sid[Line_size];
    int sMaxCap[Line_size];
    int sActCap[Line_size];
    int time[Line_size];
    char sName[Line_size];
    
   // printf("%d", lineCount);
    
    for(i=1;i<lineCount2;i++)
    {
        sscanf(ship[i],"%d %d %d %d", &sid[i], &sMaxCap[i], &sActCap[i], &time[i]); // ship
       
    } 
    
    for(i=1;i<lineCount;i++)
    {
      sscanf(harbour[i],"%d %d %d %lf", &id[i], &maxCap[i], &perUnit[i], &amount[i]);// harbour
    }

    
    // Sort Array
    qsort(time, lineCount, sizeof(int), sort);
    for(i=1;i<lineCount2;i++)
    {
         //printf("%d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]);  
        if( sMaxCap[i] <= maxCap[1])
        {
           printf("first %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[2])
        {
            printf("second %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[3])
        {
            printf("Third %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[4])
        {
            printf("Fourth %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[5])
        {
            printf("Five %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[6])
        {
            printf("Six %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[7])
        {
            printf("Serven %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[8])
        {
            printf("Eight %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[9])
        {
            printf("Night %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        if( sMaxCap[i] <= maxCap[10])
        {
            printf(" Ten %d %d %d %d\n", sid[i], sMaxCap[i], sActCap[i], time[i]); 
        }
        
        
    }
    
    return 0;
    
}
int main ( void )
{
    static const char filename[] = "data.txt";
    static const char filename2[] = "data2.txt";
    FILE *file = fopen ( filename, "r" );
    FILE *file2 = fopen ( filename2, "r" );
    int i, j, l;
    char harbour[Line_size][Line_size];
    char ship[Line_size][Line_size];
    char line[Line_size];
    char line1[Line_size];
    int lineCount = 0, lineCount2 = 0;
    
    
    for(i=0; i<sizeof lineCount; i++)
    {
        for(j=0; j<sizeof lineCount; j++)
        {
            harbour[i][j] = '\0';
        }
    }
    
    
    for(i=0; i<sizeof lineCount; i++)
    {
        line[i] = '\0';
    }
    
    
    for(l=0; i<sizeof lineCount; l++)
    {
        for(j=0; j<sizeof lineCount; j++)
        {
            ship[l][j] = '\0';
        }
    }
    
    
    for(i=0; i<sizeof lineCount; i++)
    {
        line1[i] = '\0';
    }
    
    if (( file != NULL ) && ( file2 != NULL)) 
    {
        
        i=0, l=0;
        
        while (( fgets ( line, sizeof line, file ) != NULL )) /* read a line */
        {
           strcpy(harbour[i], line);
            
            i++;
            lineCount++;
            
        }
        
        while (( fgets ( line1, sizeof line1, file2 ) != NULL )) 
        {
            strcpy(ship[l], line1);
            l++;
            lineCount2++;

        }
        fclose ( file );
        
        /* Call Function Begin Here */
        
        determindCapacity( lineCount2, lineCount, harbour, ship);
        
    }
    else
    {
        printf("File was not found\n");
    }
    
    
    return 0;
}
Re: insert multiple variable into single element in array c 80 80
Member Avatar

Why don't you use a structure instead?

# define Line_size 200

typedef struct harbor_s
{
int id;
int maxCap;
int perUnit;
double amount;
char name[<some size>];
} harbor_t;

typedef struct ship_s
{
int sid;
int sMaxCap;
int sActCap;
int time;
char sName[<some size>];
} ship_t;

harbor_t harbors[Line_size];
ship_t ships[Line_size];
Re: insert multiple variable into single element in array c 80 80

How to assign into the code ? because I dont really know how to use struct

Re: insert multiple variable into single element in array c 80 80
Member Avatar
Re: insert multiple variable into single element in array c 80 80

But is it posible to use just array

Re: insert multiple variable into single element in array c 80 80
Member Avatar

What do you mean?

Re: insert multiple variable into single element in array c 80 80

I mean if I dont want to use structure is there any other way

Re: insert multiple variable into single element in array c 80 80

anyone ? plz help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.