+ 1
please tell me how to make this C program
Write a program which will read several lines of text and determine the average number of characters(including punctuation and blank spaces) per line. First define a function which will calculate the number of characters entered per line and then use it in the main program. we can use only 1D array and no pointers.
5 ответов
+ 11
Indeed. There are tons of ways to formulate a particular problem. My example is one of them. Hope you come up with your preferred solution.
+ 11
If you happened to use string in your program, you would call a function by string argument like this (using "constant string reference" as variable type inside the parameter list - In fact string like an array is a container and normally you pass the address of the first element in the container to function):
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void avg(const string &x, const string &y, const string &z) {
int length1 = x.length();
int length2 = y.length();
int length3 = z.length();
cout << length1 << endl;
cout << length2 << endl;
cout << length3 << endl;
cout << "Average # of character in each line is about: ";
cout << round(static_cast<double>(length1 + length2 + length3) / 3.0);
}
int main()
{
string line1 = "";
string line2 = "";
string line3 = "";
cout << "Line1 is: "; getline(cin, line1);
cout << "Line2 is: "; getline(cin, line2);
cout << "Line3 is: "; getline(cin, line3);
avg(line1, line2, line3);
}
+ 10
This is a simple example for 3 lines using C++. I find it useful to share with you. Hopefully, you get some value out of it.
[https://code.sololearn.com/cJKa9nqVfUdd]
+ 1
I have to use function to calculate no of characters. and 1 1D array
+ 1
I don't know how can I pass the string to the function without pointer. and also one array is to be used. how?