Week 12 solutions 12a) Write a C++ program to display the contents of a text file. File: hello.txt Hello **** Welcome to C++ programming **** Program: #include<iostream> #include<fstream> using namespace std; int main() { ifstream in("hello.txt"); cout<<in.rdbuf(); //rdbuf()→ function outputs all the contents of the file return 0; } output: Hello **** Welcome to C++ programming ****
12 b) Write a C++ program that counts the characters, lines and words in the text file. file:wish.txt Hello Welcome to C++ programming happy coding program: #include<iostream> #include<fstream> #include<cctype> using namespace std; int main() { int data,count=0,words=0,lines=0; ifstream file; file.open("wish.txt",ifstream::in); char c=file.get(); cout<<"\nContents of the file\n"; while(!file.eof()) { cout<<c; if(isalnum(c)) count++; if(isspace(c)) words++; if(c=='\n') lines++; c=file.get(); } lines++,words++; file.close(); cout<<"\n\n Characters :"<<count; cout<<"\n Lines :"<<lines; cout<<"\n Words :"<<words; return(0); } output: Hello Welcome to C++ programming happy coding characters:37 Lines:3 words:7
12 c) Write a C++ program that produces the sum of all the numbers in a file of whitespace separated integers. File:num.txt 1 2 3 4 5 6 7 8 9 10 program: #include<iostream> #include<fstream> using namespace std; int main(void) { int sum=0; ifstream file("num.txt"); int x; while(!file.eof()) { file>>x; cout<<x<<" "; sum+=x; } file.close(); cout<<"\nSum of all integers is "<<sum; } output: 1 2 3 4 5 6 7 8 9 10 Sum of all integers is 55
Monday, 23 December 2013
week 12 oops
Saturday, 30 November 2013
OOPS ASSIGNMENT-2
OOPS ASSIGNMENT-2
1)What is a problem ? Thinking about a problem
Problem: Writing a program starts with a problem. →Understanding that problem is key to a good program. →After all, a program that solves the wrong problem is likely to be of little use to you,however elegant it may be. → What we want is a program that simply and cleanly solves the problem we decided to solve. A program that → Illustrates design and programming techniques →Gives us a chance to explore the kinds of decisions that a programmer must make and the considerations that go into such decisions →Doesn't require too many new programming language constructs →Is complicated enough to require thought about its design →Allows for many variations in its solution →Solves an easily understood problem →Solves a problem that's worth solving →Has a solution that is small enough to completely present and completely comprehendThinking about the problem: So how do we start? Basically, think a bit about the problem and how to solve it. → First think about what the program should do and how you'd like to interact with it. Later, you can think about how the program could be written to do that. →Try writing down a brief sketch of an idea for a solution, and see what's wrong with that first idea. Maybe discuss the problem and how to solve it with a friend. →Trying to explain something to a friend is a marvelous way of figuring out what's wrong with ideas, even better than writing them down; paper (or a computer) doesn't talk back at you and challenge your assumptions . Ideally, design isn't a lonely activity. →Unfortunately, there isn't a general strategy for problem solving that works for all people and all problems. →Adopt a more than usually skeptical attitude.Stages of development: Here is a bit of terminology for program development. As you work all a problem you repeatedly go through these stages: →Analysis: Figure out what should be done and write a description of your (current) understanding of that. Such a description is called a set of requirements or a specification. We will not go into details about how such requirements are developed and written down. 'That's beyond the scope of this book, but it becomes increasingly important as the size of problems increases. →Design: Create an overall structure for the system, deciding which parts the implementation should have and how those parts should communicate. As part of the design consider which tools - such as libraries – can help you Structure the program. →Implementation: Write the code, debug it, and test that it actually does what it is supposed to do.
2)Write a program to implement simple caluculator ?
#include <iostream> using namespace std; int main() { float num1,num2; char ch,t; cout<<"Enter the expression (a+b or a-b or a*b ..........)\n"; cin>>num1>>ch>>num2; switch(ch) { case '+' :cout<<num1<<ch<<num2<<"="<<num1+num2<<endl; break; case '-' :cout<<num1<<ch<<num2<<"="<<num1-num2<<endl; break; case '*' :cout<<num1<<ch<<num2<<"="<<num1*num2<<endl; break; case '/' :cout<<num1<<ch<<num2<<"="<<num1/num2<<endl; break; } return 0; }output: Enter the expression (a+b or a-b or a*b ..........) 5+2 5+2=7 Enter the expression (a+b or a-b or a*b ..........) 5-2 5-2=3 Enter the expression (a+b or a-b or a*b ..........) 7*10 7*10=70 Enter the expression (a+b or a-b or a*b ..........) 5/2 5/2=2.5
3)Describe briefly about completing a problem ?
loading progress:
Thursday, 14 November 2013
Oops Program
3a Program (OOPS)
a) Write a C++ program to read a sequence ofdouble values into a vector. Think of eachvalue as the distance between two cities along a given route. Compute and int the total distance. Find and print the smallest and greatest distance between two neighboring cities. Find and print the mean distance of the neighboring cities.Program ::
#include < iostream> #include < vector> #include < algorithm>using namespace std;int main() {double val,sum=0; vector<double >dist; cout<<"Enter the series of distances between two cities along a route \n" ;while (cin>>val&&val!=0) { dist.push_back(val); sum+=val; } cout<<"\nThe total distance is: " << sum; cout<<"\nThe max distance between two neighboring cities is " << *max_element(dist.begin(), dist.end()); cout<<"\nThe min distance between two neighboring cities is " << *min_element(dist.begin(), dist.end());return (0); }Output::
Enter the series of distances between two cities along a route 25.77 50.50 60.73 77.77 0 The total distance is: 214.7 The max distance between two neighboring cities is 77.77 The min distance between two neighboring cities is 25.77