Its not assignment, as part of my regular work I am doing it... Some how tried to achieve the same using C program with 8 input records.
#include<stdio.h>
char emp[7][3] = {{'A', 'B'},
{'A', 'C'},
{'A', 'D'},
{'C', 'E'},
{'C', 'F'},
{'X', 'Y'},
{'E', 'Z'}};
char topMgr;
char findTopMgr(char mgr, char sub)
{
int i,j;
/* printf("\n Mgr=%c, Sub=%c\n", mgr, sub);*/
for(i=0;i<7;i++)
{
if(mgr == emp[1]) //found top mgr for mgr
{
sub = mgr; // mgr becomes sub &
mgr = emp[0];
break;
}
}
if(i == 7)// not found top mgr
{
/*printf("not found top mgr for %c \n", mgr);*/
topMgr = mgr;
return mgr;
}
else
findTopMgr(mgr, sub);
}
int main()
{
int i,j,c;
char mgr,sub;
printf("Source\n");
for(i=0; i<7; i++)
{
for (j=0;j<2;j++)
{
printf("%c",emp[j]);
printf("\t");
}
printf("\n");
}
//////////////////////////////
for(i=0;i<7;i++)
{
/* printf("%d row -----------\n", i+1);*/
sub = emp[1];
mgr = emp[0];
findTopMgr(mgr, sub);
emp[2] = topMgr; // top mgr in last column
}
//////////////////////////////////
printf("Output\n");
for(i=0; i<7; i++)
{
for (j=1;j<3;j++)
{
printf("%c",emp[j]);
printf("\t");
}
printf("\n");
}
getch();
}
I am getting the desired output, but I am trying to achieve the same using shell script. Could you please help me how to use arrays in unix:
Thanks a lot
---------- Post updated at 12:55 AM ---------- Previous update was at 12:47 AM ----------
Also tried to read the data from file to provide input to the above code. Currently I am trying to integrate the below code into the previous thread code.
#include <stdio.h>
#include <string.h>
char str1[8][20], str2[8][20];
char emp[8][2][20];
void main()
{
FILE *fp = fopen("user.txt", "r");
const char s[2] = ",";
char *token;
int i, index=0;
if(fp != NULL)
{
char line[20];
while(fgets(line, sizeof(line), fp) != NULL)
{
token = strtok(line, s);
strcpy(str1[index], token);
strcpy(emp[index][0], token);
for(i=0;i<2;i++)
{
if(i==0)
{
// printf("%s ",token);
token = strtok(NULL,s);
strcpy(str2[index], token);
strcpy(emp[index][1], token);
} else {
// printf("%s\n",token);
}
}
index++;
}
fclose(fp);
} else {
perror("user.txt");
}
// printf("\n-------------------\n");
for(i=0;i<8;i++)
{
//printf("%d %s %s \n", i+1, str1, str2);
printf("%s,%s \n", emp[0], emp[1]);
}
getch();
}
My O/S is GNU LINUX & I am trying to create the same logic in korn/bash shell.