who else got tut4' answ pls send to me, and upload here for sharing together. Dun b selfish, Selfish people are in more failure...xD
if thr is any mistake in the code i uploaded, pls commant about it.
Thursday, April 30, 2009
sol tut9 ecp1026
/* t9.c : command line call using "t9 t9data" */
#include
#include
#define NDATA 10
struct ivdata{
float volt;
float amp;};
int linSearch (float, struct ivdata [],int);
int binSearch (float, struct ivdata [],int, int);
void readData (struct ivdata [],int);
void printData (struct ivdata [],int);
struct ivdata Data[NDATA];
FILE *in;
int main (int argc, char *argv[])
{
int res1,res2;
float input;
if (argc!= 2)
{ fprintf(stderr,"Usage: \n\t%s\n",argv[0]);
exit(1);}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);}
readData(Data,NDATA);
printData(Data,NDATA);
//search voltage that matched the searched current
printf("Enter the voltage value to search?\n");
scanf("%f",&input);
//Call search functions,store results in res1,res2
res1=linSearch(input,Data,NDATA);
res2=binSearch(input,Data,0,NDATA-1);
if (res1!=-1)
printf("Linear search:V=%2.2f,I=%2.2f\n",Data[res1].volt,Data[res1].amp);
else printf("Not found!\n");
if (res2!=-1)
printf("Binary search:V=%2.2f,I=%2.2f\n",Data[res2].volt,Data[res2].amp);
else printf("Not found!\n");
return 0;
}
void readData (struct ivdata data[],int N){
int j=0;
float i,v;
while (j { //read data from file, 2 data points in each iteration
fscanf(in,"%f %f",&v,&i);
data[j].volt=v;
data[j].amp=i;
j++;
}
}
//print the data in 2-column format
void printData (struct ivdata data[],int N){
int i ;
for ( i=0 ; i printf("%2.2f ; %2.2f\n",data[i].volt,data[i].amp);
}
int linSearch (float x, struct ivdata data[],int N){
int j;
for(j=0; j if( data[j].volt == x)
return(j);
}
return(-1);
}
int binSearch (float x, struct ivdata data[],int start, int end)
{
int mid=(start+end)/2;
float result=data[mid].volt;
if(result==x){ printf("%d\n", mid); return(mid);}
if(start>end) return(-1);
if(result<0)
return(binSearch(x,data,start,mid-1));
else
return(binSearch(x,data,mid+1,end));
}
#include
#include
#define NDATA 10
struct ivdata{
float volt;
float amp;};
int linSearch (float, struct ivdata [],int);
int binSearch (float, struct ivdata [],int, int);
void readData (struct ivdata [],int);
void printData (struct ivdata [],int);
struct ivdata Data[NDATA];
FILE *in;
int main (int argc, char *argv[])
{
int res1,res2;
float input;
if (argc!= 2)
{ fprintf(stderr,"Usage: \n\t%s
exit(1);}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);}
readData(Data,NDATA);
printData(Data,NDATA);
//search voltage that matched the searched current
printf("Enter the voltage value to search?\n");
scanf("%f",&input);
//Call search functions,store results in res1,res2
res1=linSearch(input,Data,NDATA);
res2=binSearch(input,Data,0,NDATA-1);
if (res1!=-1)
printf("Linear search:V=%2.2f,I=%2.2f\n",Data[res1].volt,Data[res1].amp);
else printf("Not found!\n");
if (res2!=-1)
printf("Binary search:V=%2.2f,I=%2.2f\n",Data[res2].volt,Data[res2].amp);
else printf("Not found!\n");
return 0;
}
void readData (struct ivdata data[],int N){
int j=0;
float i,v;
while (j
fscanf(in,"%f %f",&v,&i);
data[j].volt=v;
data[j].amp=i;
j++;
}
}
//print the data in 2-column format
void printData (struct ivdata data[],int N){
int i ;
for ( i=0 ; i
}
int linSearch (float x, struct ivdata data[],int N){
int j;
for(j=0; j
return(j);
}
return(-1);
}
int binSearch (float x, struct ivdata data[],int start, int end)
{
int mid=(start+end)/2;
float result=data[mid].volt;
if(result==x){ printf("%d\n", mid); return(mid);}
if(start>end) return(-1);
if(result<0)
return(binSearch(x,data,start,mid-1));
else
return(binSearch(x,data,mid+1,end));
}
sol tut7 ecp1026
#include
#define NUMPLACE 3
#define NOP -1.0
char places[NUMPLACE]={'A','B','C'};
float roads[NUMPLACE][NUMPLACE]={
{0.0,3.5,NOP},
{3.2,0.0,2.8},
{3.1,NOP,0.0},
};
int main(void){
int from;
int to;
char origin, dest;
printf("From>>");
scanf("%c",&origin);
for(from=0;from>");
scanf("%c",&dest);
for(to=0;to
if(places[to]==dest)break;
printf("From %c to %c ", places[from],places[to]);
if(roads[from][to]!=NOP)
printf("it is %2.1f km\n",roads[from][to]);
else
printf("no direct road.\n");
return(0);
}
#define NUMPLACE 3
#define NOP -1.0
char places[NUMPLACE]={'A','B','C'};
float roads[NUMPLACE][NUMPLACE]={
{0.0,3.5,NOP},
{3.2,0.0,2.8},
{3.1,NOP,0.0},
};
int main(void){
int from;
int to;
char origin, dest;
printf("From>>");
scanf("%c",&origin);
for(from=0;from>");
scanf("%c",&dest);
for(to=0;to
if(places[to]==dest)break;
printf("From %c to %c ", places[from],places[to]);
if(roads[from][to]!=NOP)
printf("it is %2.1f km\n",roads[from][to]);
else
printf("no direct road.\n");
return(0);
}
sol tut6 ecp1026
#include
#include
struct BinTreeNode {
char data;
struct BinTreeNode *left;
struct BinTreeNode *right;
};
struct BinTreeNode *initBinTreeNode(char data);
void visit(char data);
void preorder(struct BinTreeNode *node);
void postorder(struct BinTreeNode *node);
struct BinTreeNode *root, *parent1, *parent2, *parent3;
void inorder (struct BinTreeNode *node);
void SetTree();
int main(void){
SetTree();
/* traverse and print tree */
printf("\nPreorder traversal:\t"); preorder(root);
printf("\nInorder traversal:\t"); inorder(root);
printf("\nPostorder traversal:\t"); postorder(root);
return(0);
}
void visit(char data){printf("%c ", data);}
struct BinTreeNode *initBinTreeNode(char data){
struct BinTreeNode *temp;
temp = (struct BinTreeNode *)
malloc(sizeof(struct BinTreeNode));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return(temp);
}
void preorder(struct BinTreeNode *node){
if (node){ /* if Node exists */
visit(node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(struct BinTreeNode *node){
if (node){
postorder(node->left);
postorder(node->right);
visit(node->data);
}
}
void inorder(struct BinTreeNode *node){
if (node){
inorder(node->left);
visit(node->data);
inorder(node->right);
}
}
void SetTree(){
/*1*/
root=initBinTreeNode('A');
root->left=initBinTreeNode('B');
root->right=initBinTreeNode('C');
/*2*/
parent1=root->left;
parent1->left=initBinTreeNode('D');
parent1->right=initBinTreeNode('E');
/*3*/
parent2=parent1->right;
parent2->left=initBinTreeNode('H');
parent2->right=initBinTreeNode('I');
/*4*/
parent1=root->right;
parent1->left=initBinTreeNode('E');
parent1->right=initBinTreeNode('G');
/*5*/
parent2=parent1->left;
parent2->left=initBinTreeNode('J');
parent2->right=initBinTreeNode('K');
/*6*/
parent3=parent2->right;
parent3->left=initBinTreeNode('L');
parent3->right=initBinTreeNode('M');
}
#include
struct BinTreeNode {
char data;
struct BinTreeNode *left;
struct BinTreeNode *right;
};
struct BinTreeNode *initBinTreeNode(char data);
void visit(char data);
void preorder(struct BinTreeNode *node);
void postorder(struct BinTreeNode *node);
struct BinTreeNode *root, *parent1, *parent2, *parent3;
void inorder (struct BinTreeNode *node);
void SetTree();
int main(void){
SetTree();
/* traverse and print tree */
printf("\nPreorder traversal:\t"); preorder(root);
printf("\nInorder traversal:\t"); inorder(root);
printf("\nPostorder traversal:\t"); postorder(root);
return(0);
}
void visit(char data){printf("%c ", data);}
struct BinTreeNode *initBinTreeNode(char data){
struct BinTreeNode *temp;
temp = (struct BinTreeNode *)
malloc(sizeof(struct BinTreeNode));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return(temp);
}
void preorder(struct BinTreeNode *node){
if (node){ /* if Node exists */
visit(node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(struct BinTreeNode *node){
if (node){
postorder(node->left);
postorder(node->right);
visit(node->data);
}
}
void inorder(struct BinTreeNode *node){
if (node){
inorder(node->left);
visit(node->data);
inorder(node->right);
}
}
void SetTree(){
/*1*/
root=initBinTreeNode('A');
root->left=initBinTreeNode('B');
root->right=initBinTreeNode('C');
/*2*/
parent1=root->left;
parent1->left=initBinTreeNode('D');
parent1->right=initBinTreeNode('E');
/*3*/
parent2=parent1->right;
parent2->left=initBinTreeNode('H');
parent2->right=initBinTreeNode('I');
/*4*/
parent1=root->right;
parent1->left=initBinTreeNode('E');
parent1->right=initBinTreeNode('G');
/*5*/
parent2=parent1->left;
parent2->left=initBinTreeNode('J');
parent2->right=initBinTreeNode('K');
/*6*/
parent3=parent2->right;
parent3->left=initBinTreeNode('L');
parent3->right=initBinTreeNode('M');
}
sol tut5 ecp1026
#include < stdio.h >
/* This program lets you specify a 65x32 text file representing a
* picture and 2 numbers representing coordinates and then does a
* recursive area fill. (Note that 65 includes the carriage return
* so actually it looks like just 64 characters wide.) */
#define XMAX 65
#define YMAX 32
char screen[XMAX][YMAX];
void printScreen(void);
void fill(int, int);
int main(int argc, char *argv[])
{ FILE *in;
int x, y;
if (argc!= 4)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> \n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ printf("Error in opening input file.");
exit(1);
}
/* Read the 65x32 text file */
for (y=0; y< YMAX; y++)
{ for (x=0; x< XMAX; x++)
screen[x][y] = fgetc(in);
}
if (fclose(in))
{ printf("Error in closing input file.");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
fill(x,y);
printScreen();
}
void printScreen(void)
{ int x,y;
for (y=0; y< YMAX; y++)
for (x=0; x< XMAX; x++)
putchar(screen[x][y]);
}
void fill (int x, int y)
{
if (screen[x][y]!=' ')
return;
screen[x][y]='X';
fill(x, y+1);
fill(x, y-1);
fill(x+1, y);
fill(x-1, y);
}
/* This program lets you specify a 65x32 text file representing a
* picture and 2 numbers representing coordinates and then does a
* recursive area fill. (Note that 65 includes the carriage return
* so actually it looks like just 64 characters wide.) */
#define XMAX 65
#define YMAX 32
char screen[XMAX][YMAX];
void printScreen(void);
void fill(int, int);
int main(int argc, char *argv[])
{ FILE *in;
int x, y;
if (argc!= 4)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> \n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ printf("Error in opening input file.");
exit(1);
}
/* Read the 65x32 text file */
for (y=0; y< YMAX; y++)
{ for (x=0; x< XMAX; x++)
screen[x][y] = fgetc(in);
}
if (fclose(in))
{ printf("Error in closing input file.");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
fill(x,y);
printScreen();
}
void printScreen(void)
{ int x,y;
for (y=0; y< YMAX; y++)
for (x=0; x< XMAX; x++)
putchar(screen[x][y]);
}
void fill (int x, int y)
{
if (screen[x][y]!=' ')
return;
screen[x][y]='X';
fill(x, y+1);
fill(x, y-1);
fill(x+1, y);
fill(x-1, y);
}
sol tut3 ecp1026
#include
#include
#include
#define MNAME 40
struct Human{
char name[MNAME];
int id;
};
void readData(struct Human *data1,char *argv[]){
int i;
for (i=0 ; i<3; i++){
strcpy(data1[i].name,argv[2*i + 1]);
data1[i].id = atoi (argv[2*i + 2]);}
}
void printData(struct Human *data1){
int i;
for (i=0 ; i<3; i++){
printf("%s ",data1[i].name);
printf("%d\n",data1[i].id );}
}
int main(int argc, char *argv[]){
struct Human student[3];
if (argc == 7)
printf("You have entered sufficient arguments \n");
else{
fprintf(stderr, "usage %s \n", argv[0]);
return(1);
}
readData(student,argv);
printData(student);
return(0);
}
#include
#include
#define MNAME 40
struct Human{
char name[MNAME];
int id;
};
void readData(struct Human *data1,char *argv[]){
int i;
for (i=0 ; i<3; i++){
strcpy(data1[i].name,argv[2*i + 1]);
data1[i].id = atoi (argv[2*i + 2]);}
}
void printData(struct Human *data1){
int i;
for (i=0 ; i<3; i++){
printf("%s ",data1[i].name);
printf("%d\n",data1[i].id );}
}
int main(int argc, char *argv[]){
struct Human student[3];
if (argc == 7)
printf("You have entered sufficient arguments \n");
else{
fprintf(stderr, "usage %s
return(1);
}
readData(student,argv);
printData(student);
return(0);
}
sol tut 2 ecp1026
\\\\\\\\\\\\\\\\question 1\\\\\\\\\\\\\\\\\\\\
#include
#include
int main (int argc, char *argv[]){
int i,j;
int*p;
if (argc > 1){
printf("argv[1]= %s\n", argv[1]);
i = atoi(argv[1]); /* atoi() converts string to integer */
}
else
{ fprintf(stderr, "usage: %s.\n", argv[0]);
exit(1);
}
p=&i;
j= *p + 2;
*p = j / 2;
printf("i =%d\n",i);
printf("j =%d\n",j);
printf("*p =%d\n",*p);
exit(0);
return 0;}
\\\\\\\\\\\\\\\\question 2\\\\\\\\\\\\\\\\\\\\\\
#include
#include
#define SIZE 5
void Learning (void);
void Ecp1026(int data);
int sum(int b[],int size);
int main (){int i,total,a[SIZE];
Learning();
printf("Please Enter 5 int Number :");
for ( i = 0; i < SIZE; i++ ) { /* Input array data from */
scanf( "%d", &a[ i ] ); /* user one at a time */
Ecp1026(a[i]);
}
total = sum(a,SIZE);
printf("the sum of numbers that you entered is %d\n",total);
return 0 ;}
void Learning(void){ printf("lerning c is fun!!\n");
}
void Ecp1026(int data){
printf("You have passed %d as argument for this function\n",data);
}
int sum(int b[],int size){
int j,sum = 0;
for(j=0;j sum+=b[j];
return sum;}
#include
#include
int main (int argc, char *argv[]){
int i,j;
int*p;
if (argc > 1){
printf("argv[1]= %s\n", argv[1]);
i = atoi(argv[1]); /* atoi() converts string to integer */
}
else
{ fprintf(stderr, "usage: %s
exit(1);
}
p=&i;
j= *p + 2;
*p = j / 2;
printf("i =%d\n",i);
printf("j =%d\n",j);
printf("*p =%d\n",*p);
exit(0);
return 0;}
\\\\\\\\\\\\\\\\question 2\\\\\\\\\\\\\\\\\\\\\\
#include
#include
#define SIZE 5
void Learning (void);
void Ecp1026(int data);
int sum(int b[],int size);
int main (){int i,total,a[SIZE];
Learning();
printf("Please Enter 5 int Number :");
for ( i = 0; i < SIZE; i++ ) { /* Input array data from */
scanf( "%d", &a[ i ] ); /* user one at a time */
Ecp1026(a[i]);
}
total = sum(a,SIZE);
printf("the sum of numbers that you entered is %d\n",total);
return 0 ;}
void Learning(void){ printf("lerning c is fun!!\n");
}
void Ecp1026(int data){
printf("You have passed %d as argument for this function\n",data);
}
int sum(int b[],int size){
int j,sum = 0;
for(j=0;j
return sum;}
solution ecp1026 tut1
#include
int main ()
{
int pw = 2210 ,num, loop ;
char name[20] ;
while (loop=1) {
printf("Please Enter Your Name : ");
scanf("%s",name);
printf("Please Enter Your Password : ");
scanf("%d",&num);
if (num == pw ){
printf("Welcome %s to ECP 1026 Programing Class\n",name);
return 0;}
else {
printf("%s please login with correct password\n",name);
loop = 1 ;
}}
return 0 ; }
//////////////question 2//////////////////
#include
int main ()
{
int i ,j ,n[10]={0,1,2,3,4,5,6,7,8,9};
for ( i=0;i<10;i++){
for ( j = 1; j <= n[ i ]; j++ ) {
printf( "%c", '*' );
} printf( "%d",i );
printf( "\n" );
}
return 0 ; }
int main ()
{
int pw = 2210 ,num, loop ;
char name[20] ;
while (loop=1) {
printf("Please Enter Your Name : ");
scanf("%s",name);
printf("Please Enter Your Password : ");
scanf("%d",&num);
if (num == pw ){
printf("Welcome %s to ECP 1026 Programing Class\n",name);
return 0;}
else {
printf("%s please login with correct password\n",name);
loop = 1 ;
}}
return 0 ; }
//////////////question 2//////////////////
#include
int main ()
{
int i ,j ,n[10]={0,1,2,3,4,5,6,7,8,9};
for ( i=0;i<10;i++){
for ( j = 1; j <= n[ i ]; j++ ) {
printf( "%c", '*' );
} printf( "%d",i );
printf( "\n" );
}
return 0 ; }
Subscribe to:
Posts (Atom)