0

Hi friends,

I have written a C program which involves some iterations of numbers that I input in a table.

>> Now I want to know how to import these values say from excel or some text instead of me inputtin them....

Any suggestion wil bw very helpful.

Thanks and Regards,'

Arun

4
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by arunk8186
0

Well, for one thing importing from an Excel file is not as easy as it may sound. You must write a parser (or find one) that grabs the data, and it quickly turns into an icky mess for anyone unexperienced with C/C++.

If you have the numbers in a regular ASCII text file, your job is much simpler -- just use fopen/fclose and fgets to handle your input. By the way, you might be interested in a C input tutorial:
http://www.cs.bu.edu/teaching/c/file-io/intro/

0

Well, for one thing importing from an Excel file is not as easy as it may sound. You must write a parser (or find one) that grabs the data, and it quickly turns into an icky mess for anyone unexperienced with C/C++.

If you have the numbers in a regular ASCII text file, your job is much simpler -- just use fopen/fclose and fgets to handle your input. By the way, you might be interested in a C input tutorial:
http://www.cs.bu.edu/teaching/c/file-io/intro/

Hello sir, thx for ur reply...

wil jus giv u the code that has been written to open a disk file...can u plz tel me in which format the input file has to be??

******************************************************************************************
Function to read data
*****************************************************************************************/

void read_data()
{
int i,j,any;
char yesno;
char fname[15];
FILE *fp;
 clrscr();
gotoxy(10,10);
printf("Data available in disk file Y/N ");
scanf("%c", &yesno);
if(toupper(yesno)=='Y')
{
 clrscr();
gotoxy(10,10);
printf("File name to read data ");
scanf("%s",fname);
fp=fopen(fname,"r");
 clrscr();
fscanf(fp,"%d",&any);
for(i=1;i<=n;i++)
for(j=1;j=n;j++)
{
fscanf(fp,"%d",&any);
x[i] [j]=any;
}
}
else
{
 clrscr();
gotoxy(10,3);
printf("Size of the matrix");
scanf("%d",&n);
printf("\nEnter the matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&any);
x[i][j]=any;
}
 clrscr();
gotoxy(10,10);
getchar();
printf("Store data in disk file Y/N");
scanf("%c",&yesno);
if(toupper(yesno)=='Y')
{
gotoxy(10,12);
printf("File name to store data");
scanf("%s",fname);
fp=fopen(fname,"w");
fprintf(fp,"%3d",n);
for(i=1;i<=n;i++)
{
fprintf(fp,"\n");
 
for(j=1;j<=n;j++)
fprintf(fp,"%5d",x[i][j]);
}
}
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
y[i][j];
}

....jus wan to know in wat file the matrix that I need must be in and wat file the above code opens as input??.....(whether excel/text/wat)

Ur suggestion wil b of great help....


Thanks and Regards,

Arun

-1

@ WALP,

Thx for ur suggestion.....but the code that I typed seems to be already formatted above in a better way...dont no who did that but is that readable now??


Thanks and REgards,

Arun

Comments
For continuing with "ur chat speak"
0

@ WALP,

Thx for ur suggestion.....but the code that I typed seems to be already formatted above in a better way...

It does? Try actually reading the link I posted and look at your 'readable' code...

dont no who did that but is that readable now??

SOS did it, and no it's not readable.

0

Arun, you need to properly format your code. Something like this:

void read_data()
{
    int i,j,any;
    char yesno;
    char fname[15];
    FILE *fp;
    clrscr();
    gotoxy(10,10);
    printf("Data available in disk file Y/N ");
    scanf("%c", &yesno);
    if(toupper(yesno)=='Y')
    {
        clrscr();
        gotoxy(10,10);
        printf("File name to read data ");
        scanf("%s",fname);
        fp=fopen(fname,"r");
        clrscr();
        fscanf(fp,"%d",&any);
        for(i=1;i<=n;i++)
            for(j=1;j=n;j++)
            {
                fscanf(fp,"%d",&any);
                x[i] [j]=any;
            }
    }
    else
    {
        clrscr();
        gotoxy(10,3);
        printf("Size of the matrix");
        scanf("%d",&n);
        printf("\nEnter the matrix\n");
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                scanf("%d",&any);
                x[i][j]=any;
            }
        clrscr();
        gotoxy(10,10);
        getchar();
        printf("Store data in disk file Y/N");
        scanf("%c",&yesno);
        if(toupper(yesno)=='Y')
        {
            gotoxy(10,12);
            printf("File name to store data");
            scanf("%s",fname);
            fp=fopen(fname,"w");
            fprintf(fp,"%3d",n);
            for(i=1;i<=n;i++)
            {
                fprintf(fp,"\n");
                
                for(j=1;j<=n;j++)
                    fprintf(fp,"%5d",x[i][j]);
            }
        }
    }
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            y[i][j];
}

Now compare the code posted by me with the code you posted. Isn't the difference obvious ?

0

thx SOS and WALTP....sorry tht my code typin caused a lot of hardship... is it ok now??

Regards,

Arun

0
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 20

#define NEWLINE printf("\n")

/** define DBUG **/
int x[MAX] [MAX], status[MAX] [MAX];
int y[MAX] [MAX];
int if_marked_row[MAX], if_marked_col[MAX];
int n,completed;
char itemx[150],itemy[200];
void readdata();
void display_matrix();
void display_status();
void display_assignment();
void change_matrix();
void row_col_assignment(int nn);
void start_marking();
void mark_cols();
void mark_rows();
void newmatrix();
/******************************************************************************************
             status[i][j] = 1 if i is assigned to j
                 = 0 if can be assigned to j
        = -1 if i cannot be assigned to j
             if_marked_row[i] = 1 if row i is marked = 0 otherwise
    if_marked_col[i] = 1 if col i is marked = 0 otherwise
******************************************************************************************
                    Function to read data
  *****************************************************************************************/
  void read_data()
{
 int i,j,any,temp1=0,temp2=0;
 int trans[10],k=0;
 char yesno,count;
 char fname[15];
 FILE *fp;
 clrscr();
 gotoxy(10,10);
 printf("Data available in disk file Y/N ");
 scanf("%c", &yesno);
 if(toupper(yesno)=='Y')
 {
  clrscr();
  gotoxy(10,10);
  printf("enter size of matrix");
  scanf("%d",&n);
  printf("File name to read data ");
  scanf("%s",fname);
  fp=fopen(fname,"r");
  clrscr();
  while((count=fgetc(fp))!=EOF)
   {
   if(count!='\n')
   {
   temp1=count-48;
   temp2=(temp2*10)+temp1;
   }
   else
   {
   trans[k]=temp2;
   temp1=0;
   temp2=0;
   k=k+1;
   }
   }
   k=0;
   for(i=1;i<=n;i++)
   {
   for(j=1;j<=n;j++)
   {
   x[i][j]=trans[k];
   k=k+1;
   }
   }
   }
  /*fscanf(fp,"%d",&n);
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
   {
    fscanf(fp,"%d",&any);
  x[i][j]=any;
   }
 }
 */
 else
 {
  clrscr();
  gotoxy(10,3);
  printf("Size of the matrix");
  scanf("%d",&n);
  printf("\nEnter the matrix\n");
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
   {
    scanf("%d",&any);
    x[i][j]=any;
   }
   clrscr();
   gotoxy(10,10);
   getchar();
   printf("Store data in disk file Y/N");
   scanf("%c",&yesno);
   if(toupper(yesno)=='Y')
   {
    gotoxy(10,12);
    printf("File name to store data");
    scanf("%s",fname);
    fp=fopen(fname,"w");
    fprintf(fp,"%3d",n);
    for(i=1;i<=n;i++)
    {
     fprintf(fp,"\n");
     for(j=1;j<=n;j++)
      fprintf(fp,"%5d",x[i][j]);
    }
   }
 }
                 for(i=1;i<=n;i++)
      for(j=1;j<=n;j++)
       y[i][j] = x[i][j];
}
/********************************************************************************************************************
                                    Function to display the matrix
  *******************************************************************************************************************/
void display_matrix()
{
 int i,j;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   printf("%6d",x[i][j]);
  NEWLINE;
 }
 NEWLINE;
}
/********************************************************************************************************************
                                    Function to display the status matrix
  *******************************************************************************************************************/
void display_status()
{
 int i,j;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   printf("%6d",status[i][j]);
  NEWLINE;
 }
 NEWLINE;
}
/********************************************************************************************************************

                                    Function to display assignment
  *******************************************************************************************************************/
void display_assignment()
{
 int i,j;
 int tc=0;

 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
   if(status[i][j]==1)
   {
    printf("%s %d to be assigned to %s %d\n",itemx,i,itemy,j);
    tc=tc+y[i][j];
   }
   printf("Total cost = %5d\n",tc);
}

/********************************************************************************************************************

  Function to subtract,from all elements in a row the minimum element in that row and from all elements in a column
  the minimum element in that column
  *******************************************************************************************************************/
void change_matrix()
{
 int i,j;
 int rowmin,colmin;
 for(i=1;i<=n;i++)
 {
  rowmin=10000;
  for(j=1;j<=n;j++)
   if(x[i][j] < rowmin)
    rowmin=x[i][j];
   for(j=1;j<=n;j++)
    x[i][j]=x[i][j] - rowmin;
 }
 for(j=1;j<=n;j++)
 {
  colmin=10000;
  for(i=1;i<=n;i++)
   if(x[i][j] < colmin)
    colmin=x[i][j];
   for(i=1;i<=n;i++)
    x[i][j]=x[i][j]-colmin;
 }
}
/********************************************************************************************************************

                                    Function for row assignment
  *******************************************************************************************************************/

row_assignment(int nn)
{
 int i,j,k,istar,jstar;
 int nzeros;
 int assigned=0;
 for(i=1;i<=n;i++)
 {
  nzeros=0;
  for(j=1;j<=n;j++)
   if(x[i][j] == 0 && status[i][j]==0)
   {
   nzeros++;
   jstar=j;
   }
   if(nzeros==nn)
   {
   for(j=1;j<=n;j++)
   if(j==jstar)
   {
# ifdef DBUG
printf("Row %d has a single zero in column %d\n",i,j);
printf("Assigning %d to %d\n",i,j);
getch();
# endif
         status[i][j]=1;
   assigned=1;
   completed++;
   }
   else
   status[i][j]=-1;
   for(k=1;k<=n;k++)
   if(status[k][jstar] == 0)
   status[k][jstar] =-1;
   }
 }
 return(assigned);
}
/********************************************************************************************************************

                                    Function for column assignment
  *******************************************************************************************************************/
column_assignment(int nn)
{
 int i,j,k,istar;
 int nzeros;
 int assigned=0;
 for(j=1;j<=n;j++)
 {
  nzeros=0;
  for(i=1;i<=n;i++)
   if(x[i][j] == 0 && status[i][j]==0)
   {
   nzeros++;
   istar=i;
   }
   if(nzeros==nn)
   {
   for(i=1;i<=n;i++)
   if(i==istar)
   {
# ifdef DBUG
printf("Col %d has a single zero in row %d\n",j,i);
printf("Assigning %d to %d\n",i,j);
getch();
# endif
         status[i][j]=1;
   assigned=1;
   completed++;
   }
   else
   status[i][j]=-1;
   for(k=1;k<=n;k++)
   if(status[istar][k] == 0)
   status[istar][k] =-1;
   }
 }
 return(assigned);
}
/********************************************************************************************************************

                                    Function for row and column assignment
  *******************************************************************************************************************/
void row_col_assignment(int nn)
{
int i,j;
int k1,k2;
for(i=1;i<=n;i++)
{
if_marked_row[i]=0;
if_marked_col[i]=0;
for(j=1;j<=n;j++)
status[i][j]=0;
}
completed=0;
do
{
       k1=row_assignment(nn);
    k2=column_assignment(nn);
# ifdef DBUG
        printf("No. f facilities assigned = %d\n",completed);
  getch();
# endif
}while (k1 > 0 || k2 > 0);
}
/********************************************************************************************************************

                                    Function to test if an assignment has been made in a row
  *******************************************************************************************************************/
assignment_in_row(int i)
{
 int j;
 int made = 0;
 for(j=1;j<=n;j++)
  if(status[i][j] ==1)
  {
   made = 1;
   break;
  }
  return(made);
}
/********************************************************************************************************************

                                    Function that marks a row if assignment has not been made
  *******************************************************************************************************************/
void start_marking()
{
 int i,j;
 for(i=1;i<=n;i++)
  if(! assignment_in_row(i))
   if_marked_row[i]=1;
}
/********************************************************************************************************************

                                    Function to mark a column which contains 0's in marked rows
  *******************************************************************************************************************/
void mark_cols()
{
 int i,j;
 for(i=1;i<=n;i++)
  if(if_marked_row[i] ==1)
   for(j=1;j<=n;j++)
    if(x[i][j]==0 && if_marked_col[j]==0)
     if_marked_col[j]=1;
}
/********************************************************************************************************************

                                    Function which marks rows that contains 0's in marked columns
  *******************************************************************************************************************/
void mark_rows()
{
 int i,j;
 for(j=1;j<=n;j++)
  if(if_marked_col[j] == 1)
   for(i=1;i<=n;i++)
    if(x[i][j] == 0 && if_marked_row[i]==0)
     if_marked_row[i]=1;
}

/********************************************************************************************************************
 Function to draw lines through all unmarked rows and marked columns and select the minimum element which has not been 
 crossed by a line.Subtract this number from all elements which have not been crossed by a line.Add this number to  
 all elements which have been crossed by two lines.
  *******************************************************************************************************************/
void new_matrix()
{
int i,j;
int min=10000;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if( if_marked_row[i]==1 && if_marked_col[j]==0 && x[i][j] < min)
min=x[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{

   if(if_marked_row[i]==1 && if_marked_col[j]==0)
   x[i][j]=x[i][j] - min;
   if(if_marked_row[i]==0 && if_marked_col[j]==1)
   x[i][j]=x[i][j] + min;
}
}


/********************************************************************************************************************

                                    Function main
  *******************************************************************************************************************/


void main()
{
 int min;
 int i,j,k,k1,k2;
 int nz;
 int left;
 clrscr();
 gotoxy(5,10);
  printf("Name of the items to be assigned");
 scanf("%s",itemx);
 gotoxy(5,12);
 printf("Name of the items to which assignment is to be made");
 scanf("%s",itemy);
 getchar();
 read_data();
 printf("The original matrix\n");
 display_matrix();
 NEWLINE;
 nz=1;
 for(;;)
 {
  change_matrix();
  row_col_assignment(nz);
  left = 0;
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    left=left+x[i][j];
   if (left > 0)
    nz=nz+1;
   row_assignment(nz);
   nz--;
   column_assignment(nz);
   row_assignment(nz);
   if(completed==n)
    if(completed == n)
    {
     display_assignment();
     getch();
     exit(0);
    }
    else
    {
     start_marking();
                # ifdef DBUG
     printf("Marked rows are\n");
     for(i=1;i<=n;i++)
      if(if_marked_row[i]==1)
       printf("%d\n",i);
      getch();
                #endif
      mark_cols();
                # ifdef DBUG
      printf("Marked cols are\n");
       for(i=1;i<=n;i++)
      if(if_marked_col[i]==1)
       printf("%d\n",i);
      getch();
                #endif
                # ifdef DBUG
      printf("Marked rows are\n");
      for(i=1;i<=n;i++)
      if(if_marked_row[i]==1)
       printf("%d\n",i);
      getch();
                #endif
      new_matrix();
    }
                # ifdef DBUG
    printf("No of facilities assigned=.%d\n",completd);
    getch();
    display_assignment();

                # endif
    /** getch();**/
    }
}

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.