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 4 years ago.
Improve this question
Found this answer to Ch4Ex15 of Stroustrups beginner book, the question is to find the first n amount of primes:
#include "std_lib_facilities.h"
bool prime (vector<int> table, int number) {
for (int i = 0; i < table.size(); ++i)
if (number%table[i] == 0) return false;
return true;
}
int main () {
int count, next;
cout << "Input the number of primes\n";
cin >> count;
vector<int> table;
next = 2;
while (table.size() < count) {
if (prime(table,next)) table.push_back(next);
++next;
}
for (int n = 0; n < table.size(); ++n)
cout << table[n] << " ";
cout << endl;
// keep_window_open();
return 0;
}
Two things I'm struggling to understand:
Why is there a section of code outside int main at the top, is the executed after int main?
How do these statements work (are they double conditions?)
bool prime (vector<int> table, int number)
and
if (prime(table,next))
Thanks,
Sean
The things you are asking are quite fundamental to the language of C and C++. A read through the first 2-3 chapters of any good C++ textbook would answer these questions for you.
The example code defines 2 functions: prime and main.
The code outside main is the definition of a prime function. It is defined (created) there for you to call later, in the main function.
These are two separate things. The first thing you mention is the definition of the function prime, the second is the call to that function.
Related
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 4 months ago.
Improve this question
Side note: I've been trying to trace this code, but every time I end up with output, it seems
to be far more or less than the right answer which is 30. If anybody can explain in to me, I'd be more than appreciated. Thanks
Always make sure to add { and } to loops so that you avoid thinking that some instruction is within a loop when it's actually not.
The code you added in the screenshot is equivalent to the following
#include <iostream>
using namespace std;
int main() {
int t = 0;
for (int c = 0; c < 3; ++c) {
// +8
for (int v = 0; v < 4; ++v) {
for (int h = 0; h < 2; ++h) {
++t;
}
}
// +2
++t;
++t;
}
cout << t << endl;
return 0;
}
So, each iteration of the first loop (variable c) adds 10 to t. There are three iterations of the first loop, the total value added to t is 30.
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.
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 7 years ago.
Improve this question
My code is below but there is some error can anyone guide me to write logic to return continuous numbers, for example, if array[] = {1,3,5,2,3,4,7,4,5,6} then function should return 2,3,4,4,5,6 keep time complexity in mind?
#include <stdio.h>
#define max 10
int coll[max];
void call_sort(int* p) {
int i = 0, first, sec;
while (*p) {
first = *p;
p++;
sec = *p - 1;
if (first == sec) {
coll[i] = *p;
i++;
}
int j;
for (j = 0; j < max; j++) {
printf("%d ", coll[j]);
//coll=coll+1;
}
}
}
int main(void) {
printf("ya\n");
int buff[max], i;
for (i = 0; i < max; i++)
scanf("%d", buff[i]);
call_sort(&buff);
}
There are many problems with your code
Wrong parameter to scanf(), you need to pass the address of the variable to modify it inside scanf() like this
scanf("%d", &buff[i]);
and you should also check that scanf() did read the value correctly by checking it's return value.
Wrong parameter to call_sort(), this will not cause any problem in this case, but it's wrong, and this combined with the scanf() issue, means that you did not enable compiler warnings, you should.
The correct way to pass buff is simply
call_sort(buff);
Wrong while (*p) which assumes that 0 is the last element of the array.
You should probably pass the size as a parameter and write a for loop, since you are dealing with numbers and 0 is a number, if it were a text string then it would be ok to exclude 0 from the normal values and use it as a sentinel value which is done in the standard library functions for strings.
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 function which gets a number, and prints all the perfect numbers that are smaller than the number given. If there are not any, the function prints an appropriate message.
The program I have just written is compiled well, but it's working only partially. If the input number is 5, for instance, for some reason I don't get any message that there are not perfect numbers in this range (=until 5) .
Can someone please explain to me what's wrong in this program?
I would appreciate any help!
#include<iostream>
using namespace std;
void check (int num, int & j);
void main()
{
int num,j, count;
cout << "List all the perfect numbers less than: ";
cin >> num;
check (num, j);
}
void check (int num, int & j)
{
int i,sum, count=0;
for(j=2;j<num;num++)
{
sum=0;
for(i=1;i<j;i++)
{
if(j%i == 0)
sum += i;
}
if(sum == j)
{cout << j <<endl;
count+=1;
}
}
if (count==0)
cout<<"there are no perfect numbers"<<endl;
}
There is an infinite loop in your code:
for(j=2;j<num;num++)
I think it should be
for(j=2;j<num;j++)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to print the even numbers of the first 25 Fibonacci numbers. However, I think I have a problem when using a vector as a parameter for my function below. Do you see what I'm doing wrong?
#include <iostream>
#include <vector>
using namespace std;
int main(){
int j=1, k=1, sum;
vector<int> myvector(25);
for (int i=0; i<25; i++) {
//cout << j << " ";
myvector[i] = j;
sum=j+k;
j=k;
k=sum;
}
findeven(myvector);
system("pause");
}
int findeven (vector<int>){
for (int i = 0, i < 25; i++){
if (vector[i] % 2 == 0){
cout << vector[i];
}
}
else{
}
}
vector<int> is just a type name. You need to name the parameter to be able to use it. You also can't use a type name as variable as you attempt to do in your loop. Fixed code:
int findeven( vector<int> v ) {
if (v[i] % 2 == 0)
cout << v[i];
//...
}
Since you don't change the vector inside the function, it'd be a good idea to pass it by const reference to avoid copying it:
int findeven( const vector<int>& v );
You'll also need to make the function visible before you use it. Right now, it's defined after main function and you'll get an error because you're trying to call it where the compiler hasn't seen its declaration yet. Put it before main (or at least its declaration).