Week 16 solutions
16a) Write a C++ program that reads a text file and
writes out how many characters of each character
classification are in the file.
File: file.txt
HELLO world
Program:
#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{
int v[26]={0},index=0;
ifstream file("file.txt");
char c=file.get();
while(!file.eof())
{
if(isalpha(c))
{
c = tolower(c);
index = c - 'a';
v[index]++;
}
c=file.get();
}
file.close();
cout<<"\nFrequency of each character \n";
cout<<"Character Frequency\n\n";
for(int i=0;i<26;i++)
{
if(v[i]>0)
cout<<char(97+i)<<"\t "<<v[i]<<"\n";
}
return 0;
}
output:
Frequency of each character
Character Frequency
d 1
e 1
h 1
l 3
o 2
r 1
w 1
16b) Write a C++ program draw a rectangle as a rectangle and as a
polygon.Make the lines of the polygon red and the lines of the
rectangle blue.
program:
#include<graphics.h>
#include<conio.h>
void poly();
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
setcolor(BLUE);
rectangle(100,30,200,80);
poly();
getch();
closegraph();
return 0;
}
void poly()
{
setcolor(RED);
line(250,100,120,150);
line(250,100,120,300);
line(120,150,120,300);
}
output:
16c) Write a C++ program draw a 100-by-30 rectangle and place the
text “PVPSIT” inside it.
program:
#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
setcolor(GREEN);
rectangle(100,30,200,80);
setcolor(RED);
outtextxy(120,40,"PVPSIT");
getch();
closegraph();
return 0;
}
output:
No comments:
Post a Comment