trying to plus each element of the loop over the next element [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 yesterday.
Improve this question
i'm trying to plus each elemnt of the loop result over the next elemnt
Like if i have a loop it gives me the numbers between 1~5 so i need 1+2+3+4+5
I tried below code but it didn't work
#include <iostream>
using namespace std;
int main() {
for (int i =1; i <=50 ;i++){
cout<<i<<" ";
cout<<(i +=i)<<endl;
}
return 0;
}

The variable 'i' is sort of a "control" variable that maintains the count. You are adding 'i' to itself and not actually printing it. If you need to print characters like a '+' you need to use quotation marks, unless they are variables.
The code you probably want to use is -
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i=1; i<n; i++){
cout<<i<<"+";
}
cout<<n<<endl;
return 0;
}
Or if you wanted the sum of all the elements, the statement 'i+=1' actually adds 'i' to itself, this is not good as 'i' is your iterator, or a control variable of some sort that maintains your count. You should not operate over it. You will need another variable to hold the sum as you loop through.
You probably want this in this situation -
#include <iostream>
using namespace std;
int main() {
int sum=0;
for(int i=1; i<=5; i++){
sum+=i;
}
cout<<sum<<endl;
return 0;
}

Related

How to count odd or even numbers between particular elements? [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 2 years ago.
Improve this question
Hi everyone!!! I was writing contest and actually this is not difficult question but I stuck on it. Please help, here my code that passed only three tests and failed on fourth test. Answer must not use function and pointers. Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long int max=a[0],min=[0],ind1=0,ind2=0;
for(int i=0;i<n;i++){
if(a[i]>=max) {
max=a[i];
ind2=i;
}
if(a[i]<=min) {
min=a[i];
ind1=i;
}
}
int sum=0;
if(ind1<ind2)
for(int i=ind1+1;i<ind2;i++){
if(a[i]%2==0) sum++;
}
else if(ind1>ind2)
for(int i=ind2+1;i<ind1;i++){
if(a[i]%2==0) sum++;
}
cout<<sum;
}
It looks like you're trying to count the number of even elements between the min and max element in the array?
It may have failed the last test because you set initial min/max to garbage. In the case where a[0] is initialised to zero, and all the elements in the array are negative, the reported max will be incorrect, for example. You need to set max to the minimum integer and vice versa.

How can this code be improved to avoid timeout with large numbers of data entries? [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
My solution to a HackerRank problem works but times out when very large amounts of data are used, such as the following: https://hr-testcases-us-east-1.s3.amazonaws.com/9403/input11.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1565703339&Signature=JcuoWT7wKxpU3GWudO4wLNWK6Dg%3D&response-content-type=text%2Fplain
I'm quite aware that the code is far from ideal, and will probably make experienced software developers cringe... so I'm hoping it can be improved.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N;
cin >> N;
vector<int> v;
vector<string> s;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
v.push_back(a);
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
int a;
int b = 0;
cin >> a;
for (int j = 0; j < v.size(); j++) {
if(v[j]==a) {
s.push_back("Yes " + to_string(j+1));
b++;
j=v.size();
}
}
if (b==0) {
vector<int>::iterator low;
low = std::lower_bound(v.begin(), v.end(), a);
int d = low-v.begin();
d++;
s.push_back("No " + to_string(d));
}
}
for (int i = 0; i < s.size(); i++) {
cout << s[i] <<"\n";
}
return 0;
}
The problem statement is:
Ideally, I'd rather not have a completely new solution, but rather get some help making this one better.
First of all, it is always useful to analyze the performance of a software by profiling it with the adeguate tool, see here.
With just taking a look at is, there are a few points which you could optimize:
the s list is useless. You push back data on it and then print in order. Just print directly. You would save memory and the last for loop.
inside your second loop you first perform a linear search and then, if nothing is found, you use std::lower_bound which has logarithmic complexity. You could reduce the time by just looping once and looking for the element or the smallest one at the same time, reducing the amount of looping you need. You can also take advantage of the fact that the N integers are sorted to stop the search earlier.

How to input N amount of characters into string from each line of a text file? [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
Please, if you write code in your answer, write it as if you were using namespace std;. Otherwise it's difficult for me to read :)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int a; // a is amount of lines in file
string word[100];
int main()
{
ifstream fd("file.txt");
fd >> a;
for(int i=0; i<a; i++)
{
//here goes the code which inputs, let's say, 20 first characters of a line into string array - word[i].
}
fd.close();
return 0;
}
I'd take into account that the file might have more than 100 lines (which would then exceed your word-array, and I'd also take into account that the file might be inconsistent concerning number of lines stated in a and actual number of lines. Given that, try the following:
string line;
int i;
for(i=0; getline(fd, line) && i<a && i < 100; i++) {
word[i] = line.substr(0,20);
}
// at this point, "i" will hold the number of items actually written to "word".

Hint: Missed newline ? while trying to solve the below quiz [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
Problem
This is similar to the previous problem, however there will be multiple integers in the input. You have to write a computer program to read each integer and print Even if the integer is divisible by 2, else print Odd. To help further, the number of integers (T) to read will be the first input to the computer program.
Input Format:
First line of input contains count of integers: T. T>=1
After that, each line contains the integer N.
Sample Input:
2
4
5
Sample Output:
Even
Odd
Note: There should be a newline after each output. Otherwise you might end up printing EvenOdd here, which will result in wrong answer.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0) cout<<"Even"<<endl;
else cout<<"Odd"<<endl;
}
return 0;
}
You don't need neither array nor vectors for this:
int main() {
int n;
cin >> n;
while(n--) {
int number;
cin >> number;
// do something with number
}
return 0;
}
Note that this structure is the most common to solve this kind of problems
your problem will be solved if you change
for(int i =0; i <sizeof(a)/sizeof(int); i++)
to
for(int i =1; i <sizeof(a)/sizeof(int); i++)
But, how'd you know the number of elemnts for the array beforehand?
To correct this, you don't declare the array statically. Take the first input, and allocate memory for that many ints.
#include <iostream>
using namespace std;
int main()
{
int a[3];
cin>>a[0];
cin>>a[1];
cin>>a[2];
for(int i =0; i <sizeof(a)/sizeof(int); i++)
{
if (a[i]%2 == 0)
cout<<"Even"<<endl;
else
cout<<"Odd"<<endl;
}
return 0;
}
this code which u posted is working perfectly bro :)

srand(time(NULL)) not giving strings anymore [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 9 years ago.
Improve this question
As a beginner in programming I can not find why srand would just not change the string anymore.
#include <stdlib.h>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int v[100], i, p, n,
srand(time(NULL));
cout<<"Numbers of numbers: "; cin>>n; cout<<endl;
for(i=0; i<=n-1; i++)
v[i]=rand()%100;
cout<<"Numbers: "<<endl;
for(i=0; i<=n-1; i++)
cout<<v[i]<<" ";
for(i=0; i<=(n-1)/2; i++){
p=v[i];
v[i]=v[n-(i+1)];
v[n-(i+1)]=p;
}
cout<<"Reversed numbers: "<<endl;
for(i=0; i<=n-1; i++)
cout<<v[i]<<" ";
return 0;
}
Tried rebuilding it, rewriting it from 1, and such. Even if it did work perfectly before it's just one type of bug that yeah, simply won't work.
Edit: Weird because I copy pasted the beginning and it worked with that... I guess it was my mistake. Not paying enough attention T_T
If you enable more warnings you will see something like
program.cpp:11:1: warning: unused variable 'srand' [-Wunused-variable]
srand(time(NULL));
^
The reason can be seen on the previous line: It ends in a comma
int v[100], i, p, n,
// Note comma here ^
So what the code is doing is declaring a variable named srand, and not calling a function called srand.
Take this as a lesson to always build with extra warnings enabled. Enabling more warnings made this problem very obvious, but it can also give hints about cases of undefined behavior.