C++ need find symbol from array [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a program that searches how many comments (// symbols) are in .txt file.
This is my code:
#include <fstream>
using namespace std;
const char read[] = "read.txt";
const char result[] = "result.txt";
const int CMax = 256;
void Skaityti (char E[], int& n);
int main() {
char E[CMax];
int n,k;
Skaityti(E,n);
ofstream rs(result);
rs << k;
return 0;
}
void Skaityti (char E[], int & n)
{
ifstream fd(read);
int k;
char sim = '/';
for (n = 0; !fd.eof() && n < n+1; n++)
fd.get(E[n]);
for(int i = 0; i < n;i++) {
if(sim == E[n])
k++; }
fd.close();
}
Program read fine, but I can't get symbol from massive.

I'm confused by your question... but I think you're asking to see how many times "//" appears in a file. I threw this together:
#include <fstream>
using namespace std;
int Skaityti()
{
ifstream fd("test.txt");
int count = 0;
while(fd.good())
{
char c = fd.get();
if(fd.good())
if(c == '/')
{
c = fd.get();
if(fd.good())
if(c == '/')
count++; // At this point we have two comments in a row
}
}
fd.close();
return count;
}

Related

Is there any way I can generate the following series? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Consider the following series sk=s(k-1)(!s(k-1))(!s(k-1))s(k-1). Take s1=1221 and !s1=2112, therefore s2=s1(!s1)(!s1)s1, which means s2=1221211221121221.
My current goal is to generate n elements of this series and then to determine the n-th element. I'm sorry if I made the question sound complicated.
I tried making this work but the code just doesn't show the correct answer or it doesn't work at all. I'd hope to see another perspective on this problem.
This is my code:
#include <iostream>
using namespace std;
int main()
{
int n,k=1,a,b,s=1,p=1,i=1;
a=1;b=2;
cin>>n;
while(i<=n){
if(s==1){
if(k==1){
cout<<a;
i++;
k++;
}
else{if(k==2||k==3){
cout<<b;
i++;
k++;
}
if(k==4){
cout<<a<<" ";
i++;
k=1;p++;
if(p==2) {s=2;p=0;}
}
}}
if(i<=n){
if(s==2){
if(k==1){
cout<<b;
i++;
k++;
}
else{if(k==2||k==3){
cout<<a;
i++;
k++;
}
if(k==4){
cout<<b<<" ";
i++;
k=1;p++;
if(p==2) {s=1;p=0;}
}
}
}
}}
return 0;
}
Here is one possible solution for your sequence generator.
#include <iostream>
#include <string>
// generate inverted term s -> !s
std::string invert(const std::string& s) {
const char a = '1';
const char b = '2';
std::string out;
for (int i = 0; i < s.size(); i++) {
// here one can check for invalid input as well, eg 0
out += s[i] == a ? b : a;
}
return out;
}
// sk = s(k - 1)(!s(k - 1))(!s(k - 1))s(k - 1)
std::string nextSeq(const std::string& s) {
std::string s_inv = invert(s);
return s + s_inv + s_inv + s;
}
// generate nth term of the sequence.
std::string nthSeq(const std::string& s, const int n) {
std::string out = s;
for (int i = 1; i < n; i++) {
out = nextSeq(out);
}
return out;
}
int main() {
std::string s1 = "1221";
// this will 2nd term 1221211221121221
std::cout << nthSeq(s1, 2) << std::endl;
return 0;
}

newbie ask about decimal to binary in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
this program suppose to convert decimal to binary but somehow i screw it up
can some one point out the error for me?
thanks a lot
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0)) {
b[q]=a%2;
a=a/2;
q++;
}while(a>0);
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[q]);
}
}
Corrected code is:
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0) {
b[q]=a%2;
a=a/2;
q++;
}
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[i]);
}
}
You were printing b[q] instead of b[i]
There are some problems with your code:
you added an extra ")" of the first while;
the second 'while' is useless (the code is being repeated due to the first one)
you are not printing the elements you want (you should use var 'i'), what you are really printing is the value after the last 0/1 (because you are using 'q')
code should look like this:
#include <conio.h>
#include <stdio.h>
int main() {
int a;
int b[20];
int q = 0;
printf("decimal: ");
scanf("%d", &a);
while (a > 0) {
b[q] = a % 2;
a = a / 2;
q++;
}
printf("binary: ");
for (int i = q - 1; i >= 0; i--) {
printf("%d", b[i]);
}
}

Skipped condition and I don't know why [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

C++ program calculates n times n for the numbers 1 to 9 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have to write a program that calculates n times n for the numbers between 1 and 9 using a function.
The output should be like 1, 4, 27, 256...
I can feel I'm very close to finishing it but I just can't figure out what the problem is, here is the code I wrote:
#include <iostream>
using namespace std;
int result, number, n;
void function1()
{
result = number;
for (int x = 1; x < number; x++)
{
result = number*result;
}
}
int main()
{
for (n = 1; n < 10; n++)
{
function1();
cout << result << endl;
system("pause");
return 0;
}
}
Try this :-
#include <iostream>
#include <math>
using namespace std;
int result, number, n;
void function1(int number)
{
int result;
result = pow(number,number);
cout<<result;
}
int main()
{
for (n = 1; n < 10; n++)
{
function1(n);
system("pause");
}
return 0;
}

how to get the last value of a vector [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to say if the value of the last element in the vector = 4 cout<<"Yes" how can i write the code write if(v.size()==y) where y is a number but it dosen't work i begin recently at writing codes
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int n,x,y;
cin >> n >> x >> y;
vector<int> v(n);
for(int i = 0; i < n; i++)
{
cin >> v[i];
}
for(int i = 0; i < n; i++)
{
if(v[0] == x)
{cout<<"EASY";
return 0;
}
if(v[0] == x && v.back() == y)
{
cout << "BOTH";
return 0;
}
if(v.back()==y)
{
cout << "HARD";
return 0;
}
if(v[0] != x && v.back() != y)
{
cout << "OKAY";
return 0;
}
}}
v.size() will returns the number of the elements in v, not the last element in the vector.
To get the last value of a vector, you can simply call std::vector::back().
Of course, you have to make sure the vector is not empty before that by checking std::vector::empty.
if (!v.empty() && v.back()==y)
{
...
}