PDA

View Full Version : Please do these simple C programs for me...


ajaybc
22-03-2008, 05:52 PM
I have to submit my record book this Monday and I have these programs pending.Please do these programs for me as I couldn't get it right when I did it myself.


1.Copy a text file to another in such a way that upper case characters are converted to lowercase vice versa giving the file name of the first file as arguments.

2.Store student details in a file such as name,roll no.,marks and age in a file and find the average age of the students and print the name of the student with the maximum mark.

3.To sort a list of names using a pointer array.

4.To print the sine series.


5.To accept the student details of n students and print the details of the youngest student using structures and functions.


Please use simple programming.
Please post these before Monday

PLEASE....

sakumar79
22-03-2008, 06:33 PM
Please post your program code and the members will help you sort it out... Please dont ask others to do your homework... That is not the right way to learn...

Arun

ajaybc
23-03-2008, 10:14 AM
Please post your program code and the members will help you sort it out... Please dont ask others to do your homework... That is not the right way to learn...

Arun

Thanks bro for ur suggestion
I am doing the programs now.
Now while doing the 5th program- "To accept the student details of n students and print the details of the youngest student using structures and functions." iam not able to get the correct output.

Please debug it for me.
The code is as shown below:


#include<stdio.h>
typedef struct st
{
int roll,age,marks;
char name[20];
}st;

main()
{
st stud[20];
int i,n;
clrscr();
printf("Enter the no. of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the details of student no.%d:\n",i+1);
printf("Name:");
scanf("%s",stud[i].name);
printf("Roll no:");
scanf("%d",stud[i].roll);
printf("Marks:");
scanf("%d",stud[i].marks);
printf("Age:");
scanf("%d",stud[i].age);
}
f1(stud,n);
}

f1(st s[],int n)
{
int young,i;
young=0;
for(i=0;i<n;i++)
{
if(s[young].age>s[i].age)
young=i;
}
printf("Youngest student is student no:%d\n",young+1);
printf("Name:%s\nRoll no:%d\nAge:%d\nMarks:%d",s[young].name,s[young].roll,s[young].age,s[young].marks);
getch();
}


QwertyManiac
23-03-2008, 10:37 AM
You've just missed the & operators in your scanf() function calls.

Here's the fixed code (Still non-standard, lest you whine and rest too):
#include<stdio.h>

typedef struct st
{
int roll,age,marks;
char name[20];
}st;

main()
{
st stud[20];
int i,n;
printf("Enter the no. of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the details of student no.%d: ",i+1);
printf("\nName: ");
scanf("%s",stud[i].name);
printf("Roll no:");
scanf("%d",&stud[i].roll);
printf("Marks:");
scanf("%d",&stud[i].marks);
printf("Age:");
scanf("%d",&stud[i].age);
}
f1(stud,n);
return 0;
}

f1(st s[],int n)
{
int young,i;
young=0;
for(i=0;i<n;i++)
{
if(s[young].age>s[i].age)
young=i;
}
printf("\nYoungest student is student no:%d\n",young+1);
printf("Name:%s\nRoll no:%d\nAge:%d\nMarks:%d\n",s[young].name,s[young].roll,s[young].age,s[young].marks);
getchar();
}

String input doesn't require an Ampersand in a scanf() statement, but rest all do, arrays or not.

mehulved
23-03-2008, 10:55 AM
Explain non-standard part?

T159
23-03-2008, 11:32 AM
Explain non-standard part?

int main

return 0;

ajaybc
23-03-2008, 11:41 AM
You've just missed the & operators in your scanf() function calls.

Here's the fixed code (Still non-standard, lest you whine and rest too):
#include<stdio.h>

typedef struct st
{
int roll,age,marks;
char name[20];
}st;

main()
{
st stud[20];
int i,n;
printf("Enter the no. of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the details of student no.%d: ",i+1);
printf("\nName: ");
scanf("%s",stud[i].name);
printf("Roll no:");
scanf("%d",&stud[i].roll);
printf("Marks:");
scanf("%d",&stud[i].marks);
printf("Age:");
scanf("%d",&stud[i].age);
}
f1(stud,n);
return 0;
}

f1(st s[],int n)
{
int young,i;
young=0;
for(i=0;i<n;i++)
{
if(s[young].age>s[i].age)
young=i;
}
printf("\nYoungest student is student no:%d\n",young+1);
printf("Name:%s\nRoll no:%d\nAge:%d\nMarks:%d\n",s[young].name,s[young].roll,s[young].age,s[young].marks);
getchar();
}String input doesn't require an Ampersand in a scanf() statement, but rest all do, arrays or not.



Thanks for the program.It works fine.

I now did the 1st program now.
"Copy a text file to another in such a way that upper case characters are converted to lowercase vice versa giving the file name of the first file as arguments."

#include<stdio.h>
main(int argc,char *argv[])
{

FILE *fone,*ftwo;
char a;
clrscr();
fone=fopen(argv[1],"w");
printf("Enter the contents of %s:\n",argv[1]);

while((a=getchar())!=EOF)
{
fputc(a,fone);
}

fclose(fone);


fone=fopen(argv[1],"r");
ftwo=fopen(argv[2],"w");
while((a=fgetc(fone))!=EOF)
{
if(isupper(a))
fputc(tolower(a),ftwo);

else if(islower(a))
fputc(toupper(a),ftwo);

}

fclose(fone);
fclose(ftwo);
ftwo=fopen(argv[2],"r");
while((a=fgetc(ftwo)!=EOF))
{
printf("%c",a);
}
fclose(ftwo);
getch();
}




everything in this program works fine.it creates two files and copies the contents and converts uppercase and lower case.
The problem comes only when it is displaying the contents of the second file.It shows some rubbish.Please debug this

QwertyManiac
23-03-2008, 11:59 AM
int main

return 0;

That wasn't what he asked.

@mehulved - I gave it in non-standard to focus on his problem first here and then show him the standard way (Which only ensures portability). I've noticed that once standards get in the discussions here the posts shift towards bad-mouthing the non-standard coders and not helping him analyze/solve issues with his code. How much have you got AGAINST portability? Give one some hospitality before showing him the door. :p

@OP - That program doesn't show any character here at all. Also, asking the user to input an EOF is a bad idea. Try with the line feed input, '\n' | Return Key.

ajaybc
23-03-2008, 12:22 PM
Instead of fighting please help me:(

I just did the program for finding the sine series upto n terms:

#include<stdio.h>
#include<math.h>
main()
{
int i,j,k,x,n,sign=-1,f=1,p=1,m;
clrscr();
printf("Enter the angle: ");
scanf("%d",&x);
printf("Enter number of terms: ");
scanf("%d",&n);
x=x*(3.14/180);
m=x;
for(i=1,j=3;i<=n;i++,j=j+2)
{
f=1;
for(k=1;k<=j;k++)
{
f=f*k;
}
p=pow(x,j);
m=m+((p/f)*sign);
sign=sign*(-1);
}
printf("%d",m);
getch();
}


I get the correct answer(which is 1) for the angle 90degree only.for all others I get 0.So I changed the int values to float.Then the answer for all angles changes to 0 which is wrong.


Please help me.
Iam in desperate need for help.I have to submit these tomorrow.

ajaybc
23-03-2008, 07:08 PM
Please guys please.Iam begging u.
I want the programs debugged before 7AM tomorrow before i go to college.
Please debug these........

mehulved
23-03-2008, 09:00 PM
#include<stdio.h>
main(int argc,char *argv[])
{

FILE *fone,*ftwo;
char a;
clrscr();
fone=fopen(argv[1],"w");
printf("Enter the contents of %s:\n",argv[1]);

while((a=getchar())!=EOF)
{
fputc(a,fone);
}

fclose(fone);


fone=fopen(argv[1],"r");
ftwo=fopen(argv[2],"w");
while((a=fgetc(fone))!=EOF)
{
if(isupper(a))
fputc(tolower(a),ftwo);

else if(islower(a))
fputc(toupper(a),ftwo);

}

fclose(fone);
fclose(ftwo);
ftwo=fopen(argv[2],"r");
while((a=fgetc(ftwo)!=EOF))
{
printf("%c",a);
}
fclose(ftwo);
getch();
}




everything in this program works fine.it creates two files and copies the contents and converts uppercase and lower case.
The problem comes only when it is displaying the contents of the second file.It shows some rubbish.Please debug this
Both the files come fine for me. Only problem is, if I input a flaot or an int I don't see it reflected into the second file. Time to create version 2.

Instead of fighting please help me:(

I just did the program for finding the sine series upto n terms:

#include<stdio.h>
#include<math.h>
main()
{
int i,j,k,x,n,sign=-1,f=1,p=1,m;
clrscr();
printf("Enter the angle: ");
scanf("%d",&x);
printf("Enter number of terms: ");
scanf("%d",&n);
x=x*(3.14/180);
m=x;
for(i=1,j=3;i<=n;i++,j=j+2)
{
f=1;
for(k=1;k<=j;k++)
{
f=f*k;
}
p=pow(x,j);
m=m+((p/f)*sign);
sign=sign*(-1);
}
printf("%d",m);
getch();
}


I get the correct answer(which is 1) for the angle 90degree only.for all others I get 0.So I changed the int values to float.Then the answer for all angles changes to 0 which is wrong.


Please help me.
Iam in desperate need for help.I have to submit these tomorrow.
Did you change all %d to %f?

And please try to use better variable names. It will makes things easier for all of us.

sakumar79
23-03-2008, 09:18 PM
Ensure i,j,k, n and sign are integers and the rest are floats... When you do calculation across types, ensure that you do conversion (f=f*(float )k)... Since I am not having a compiler at hand, I am unable to check the code in detail...

Arun

ajaybc
24-03-2008, 06:47 AM
Both the files come fine for me. Only problem is, if I input a flaot or an int I don't see it reflected into the second file. Time to create version 2.


Did you change all %d to %f?

And please try to use better variable names. It will makes things easier for all of us.

Thanx bhai that sine program worked.but that file program shows some rubbish when i try to read using this program.

sakumar79
24-03-2008, 07:10 AM
Change


if(isupper(a))
fputc(tolower(a),ftwo);

else if(islower(a))
fputc(toupper(a),ftwo);


to


if(isupper(a))
fputc(tolower(a),ftwo);

else if(islower(a))
fputc(toupper(a),ftwo);

else fputc(a,ftwo);


so that any non-alphabet characters are printed as they are...

Arun

mehulved
24-03-2008, 08:46 AM
#include<stdio.h>
main(int argc,char *argv[])
{

FILE *fone,*ftwo;
char a;
fone=fopen(argv[1],"w");
printf("Enter the contents of %s:\n",argv[1]);

while((a=getchar())!=EOF)
{
fputc(a,fone);
}

fclose(fone);


fone=fopen(argv[1],"r");
ftwo=fopen(argv[2],"w");
while((a=fgetc(fone))!=EOF)
{
if(isupper(a))
fputc(tolower(a),ftwo);

else if(islower(a))
fputc(toupper(a),ftwo);

else fputc(a,ftwo);
}

fclose(fone);
fclose(ftwo);
ftwo=fopen(argv[2],"r");
while((a=fgetc(ftwo)!=EOF))
{
printf("%c\n",a);
}
fclose(ftwo);
}

Works perfectly for me after making the change given by arun. I am using gcc under gentoo/freebsd.

ajaybc
28-03-2008, 12:55 PM
Thannx friends.MISSION ACCOMPLISHED!!!RECORD SUBMITTED!!!:D