Please do these simple C programs for me...

Status
Not open for further replies.

ajaybc

Youngling
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....
 
Last edited:

sakumar79

Technomancer
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
 
OP
ajaybc

ajaybc

Youngling
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.name);
printf("Roll no:");
scanf("%d",stud.roll);
printf("Marks:");
scanf("%d",stud.marks);
printf("Age:");
scanf("%d",stud.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.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

Commander in Chief
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):
Code:
#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",[B]&[/B]stud[i].roll);
        printf("Marks:");
        scanf("%d",[B]&[/B]stud[i].marks);
        printf("Age:");
        scanf("%d",[B]&[/B]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.
 
OP
ajaybc

ajaybc

Youngling
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):
Code:
#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",[B]&[/B]stud[i].roll);
        printf("Marks:");
        scanf("%d",[B]&[/B]stud[i].marks);
        printf("Age:");
        scanf("%d",[B]&[/B]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

Commander in Chief
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.
 
OP
ajaybc

ajaybc

Youngling
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.
 
Last edited:
OP
ajaybc

ajaybc

Youngling
Please guys please.Iam begging u.
I want the programs debugged before 7AM tomorrow before i go to college.
Please debug these........
 

mehulved

18 Till I Die............
#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.
 
Last edited:

sakumar79

Technomancer
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
 
OP
ajaybc

ajaybc

Youngling
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

Technomancer
Change

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

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

to

Code:
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

18 Till I Die............
#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.
 
Status
Not open for further replies.
Top Bottom