I created a function that finds prime numbers and I need help writing those numbers to a file in a well-formatted table with columns and rows. I think I'll need some nested loops, but I don't know what could be the best implementations. Here is my code so far, I hope you guys could help me. Thanks
Right now what my code does is write the number 2 and 3 to the file and then it goes on an infinite loop of empty lines.
The file should look like this:
2 3 5 11 13 17 19 23
29 31 37 41 43 47 53
... All the way up to closest prime number to 4000
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
bool isPrime(int);
int main()
{
int counter = 0;
int currentNumber = 2;
ofstream outputFile("test.txt");
for (int i = 2; i < 4000; i++)
{
for (int row = 1; row < 4000; row++)
{
for (int col = 1; col <= 8; col++)
{
if (isPrime(i) == true)
{
outputFile << endl;
outputFile << setw(8);
outputFile << i;
outputFile << setw(10);
i++;
}
}
outputFile << endl;
}
}
system("pause");
return 0;
}
bool isPrime(int number)
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
You're so close. Just count up to 8 primes and print out a new line, then reset the counter until the next 8. You have duplicated the i++ inside the loop as well as on the "for" line, the row and col stuff is all unecessary.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
bool isPrime(int number)
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int counter = 0;
ofstream outputFile("test.txt");
for (int i = 2; i < 4000; i++)
{
if (isPrime(i) == true)
{
outputFile << setw(8);
outputFile << i;
outputFile << setw(10);
counter++;
}
if (counter == 8)
{
outputFile << endl;
counter = 0;
}
}
return 0;
}
Richq already gave you a better way to implement this but lets look at what you did wrong in your code (looking at your intent, not always what it does):
Your outer loop goes from 2 to 4000. For each number you then print a table. (Not really since you increment i inside the table too but lets ignore that.) This is somewhat wrong. You only ever want one table so the outer loop has to go. Keep the int i = 2; though.
Second loop: row goes from 1 to 4000, printing 8 prime numbers per line. But you only want numbers smaller than 4000. Clearly 4000 * 8 primes will be larger. You don't know how many lines of table you will need. Your exit condition for the rows should be i < 4000 instead.
Third loop: col goes from 1 to 8. But maybe on the last line i will exceed 4000 on the 3rd number. So if i < 4000 is a hard limit (instead of printing till the end of the line even if you already reached 4000) then you need to include that in the condition: (col <= 8) && (i < 4000).
Inner part has 2 problems:
You print a new line before every prime number. That's why you get a single 2 and 3 in a line instead of both in the same line.
You increment i only if i is a prime number. So after 2 and 3 your increment i. But then you have 4 and the rest of your table will just test that 4 is not a prime and print nothing.
Move the newline out of the inner loop. You need to only do that after each row. And inside the row always increment i. But only increment col if you found a prime number.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
bool isPrime(int);
int main()
{
int counter = 0;
int currentNumber = 2;
ofstream outputFile("test.txt");
int i = 2;
for (int row = 1; i < 4000; row++)
{
for (int col = 1; (col <= 8) && (i < 4000); i++)
{
if (isPrime(i) == true)
{
outputFile << setw(8);
outputFile << i;
outputFile << setw(10);
col++;
}
}
outputFile << endl;
}
return 0;
}
bool isPrime(int number)
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
Related
ive got the below code to print a pattern (attached below). However i'd like to just use one loop
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<"*";
}
for(int j=1;j<=n-i;j++){
if(j%2!=0){
cout<<"_";
}else{
cout<<".";
}
}
cout<<endl;
}
for(int i=1;i<n;i++){
for(int j=1;j<=n-i;j++){
cout<<"*";
}
for(int j=1;j<=i;j++){
if(j%2==0){
cout<<".";
}else{
cout<<"_";
}
}
cout<<endl;
}
}
when n = 5, heres the output.
*_._.
**_._
***_.
****_
*****
****_
***_.
**_._
*_._.
how do i just make this into one single loop
Try this and see how it does what you want to understand the step you did not find on your own:
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n*2-1; i++) {
if (i <= n)
{
for (int j = 1; j <= i; j++) {
cout << "*";
}
for (int j = 1; j <= n - i; j++) {
if (j % 2 != 0) {
cout << "_";
}
else {
cout << ".";
}
}
cout << endl;
}
else
{
for (int j = 1; j <= n*2 - i; j++) {
cout << "*";
}
for (int j = 1; j <= i-n; j++) {
if (j % 2 == 0) {
cout << ".";
}
else {
cout << "_";
}
}
cout << endl;
}
}
}
I'd like to just use one loop.
I'll take it literally and show a starting point for a possible solution.
// Let's start by figuring out some dimensions.
int n;
std::cin >> n;
int height = 2 * n - 1;
int area = n * height;
// Now we'll print the "rectangle", one piece at a time.
for (int i = 0; i < area; ++i)
{ // ^^^^^^^^
// Extract the coordinates of the char to be printed.
int x = i % n;
int y = i / n;
// Assign a symbol, based on such coordinates.
if ( x <= y and x <= height - y - 1 )
{ // ^^^^^^ ^^^^^^^^^^^^^^^^^^^ Those are the diagonals.
std::cout << '*'; // This prints correctly the triangle on the left...
}
else
{
std::cout << '_'; // <--- But of course, something else should done here.
}
// End of row.
if ( x == n - 1 )
std::cout << '\n';
}
If you look at the pattern, then you can see a sort of "triangles". And this already gives a hint for the solution. Use a triangle function.
Please read about it here.
Then you will notice that always the "aboslute"-function, in C++ std::abs, is involved.
But first of all, it is easily visible that the number rows to print is always the width of a triangle * 2.
And the number of charcters in the pattern, can be calculated by applying the triangle function. Example for width 5:
Number of stars number of dashdot
Row width-abs(row-width) abs(row-width)
1 1 4
2 2 3
3 3 2
4 4 1
5 5 0
6 4 1
7 3 2
8 2 3
9 1 4
And this can be implemented easily now.
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std::string_literals;
int main() {
// Get the max width of the pattern and perform a short input validation
int maxWidth{};
if ((std::cin >> maxWidth) and (maxWidth > 0)) {
// The number of rows for the pattern is dependent on the width. It is a simple relation
const int numberOfRows = 2 * maxWidth;
// Show all rows
for (int row = 1; row < numberOfRows; ++row) {
// Use triangle formular to create star pattern
std::string starPattern(maxWidth - std::abs(row - maxWidth), '*');
// Create dashDot pattern
std::string ddp(std::abs(row - maxWidth), '\0');
std::generate(ddp.begin(), ddp.end(), [i = 0]() mutable { return i++ % 2 ? '.' : '_'; });
// Show output
std::cout << (starPattern+ddp) << '\n';
}
}
else std::cout << "\n*** Error: Invalid input\n\n";
}
Of course you can also create the whole pattern wit std::generate.
Maybe it is too complex for now.
And less understandable.
See:
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
int main() {
// Get the max width of the pattern and perform a short input validation
if (int width{}; (std::cin >> width) and (width > 0)) {
std::vector<std::string> row(2 * width - 1);
std::for_each(row.begin(), row.end(), [&, r = 1](std::string& s) mutable {
s = std::string(width - std::abs(r - width), '*');
std::string ddp(std::abs(r++ - width),'\0');
std::generate(ddp.begin(), ddp.end(), [&, i = 0]() mutable{ return i++ % 2 ? '.' : '_'; });
s += ddp; std::cout << s << '\n'; });
}
else std::cout << "\n*** Error: Invalid input\n\n";
}
So recently i got a programing task from a contest and my code should display the N
even digit number from 0. In even digit numbers all numbers must be dividable by 2. When i input the number 12 it should show the number 44 on the output but it shous some kind of a mess. here's my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int kturaParzysta = 1;
int N;
cin >> N;
int tab[N];
for(int i = 0; i < N;i++){
string cyfraStr = to_string(N);
bool parzystocyfrowa = true;
for(int j = 0;j <= cyfraStr.length();j++){
if(int(cyfraStr[j]) % 2 != 0){
parzystocyfrowa = false;
}
}
if(parzystocyfrowa){
tab[kturaParzysta] = i;
kturaParzysta++;
}
}
cout << tab[N - 1];
return 0;
}
so i am creating a sudoku validity checker program in C++. the program takes in a csv file with an already completed sudoku board. the program is supposed to read the file line by line and put the numbers into a 2d array (which it's doing just fine). i'm getting stuck on the checking whether or not there are any duplicate numbers in a row (i assume that if i can get this working then the columns should be fairly similar). i know how the algorithm is supposed to work in my head but i just can't seem to get it into code.
Algorithm:
1) look at each row and check for numbers 1 through 9 making sure that they only appear once (if at all)
2) if a duplicate is found (which means that some number is missing) tell the user at what row and column the error was found.
3) otherwise move on to the next row and do it again
i think that it's supposed to be a for loop running this but it's been a long time since i've coded in C++ and nothing on the internet has been very helpful to me.
Any help is appreciated.
Here is my code so far:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <thread>
using namespace std;
int *board[9];
int row, col;
void printBoard();
void is_row_ok();
int main()
{
for (int i = 0; i < 9; ++i)
{
board[i] = new int[9];
}
printBoard();
is_row_ok();
cout << endl;
return 0;
}
void printBoard()
{
string line;
string val;
ifstream myFile("Testfile1.txt");
for (int row = 0; row < 9; ++row)
{
string line;
getline(myFile, line);
if (!myFile.good())
break;
stringstream iss(line);
cout << endl;
for (int col = 0; col < 9; ++col)
{
string val;
getline(iss, val, ',');
if (!iss.good())
break;
stringstream convertor(val);
convertor >> board[row][col];
cout << board[row][col] << " ";
}
}
cout << endl;
cout << endl;
}
void is_row_ok()
{
bool found = false;
int i, j;
for (int i = 0; i < 10; ++i) //<------ edit starts here
{
int counter = 0;
for (int j = 0; j < 9; ++j)
{
if(board[row][j] == i)
{
cout << "Before " << counter << endl;
counter++;
cout << counter << endl;
}
if(counter > 1)
break;
}
}
}
The easiest way I can think of is 2 loops (one inside of another): 1 gets you through numbers 1 to 9 and the other goes through the elements of the row. Small tip with code.
for(int i=1;i<10;i++){
int counter =0;
for(int j=0;j<9;j++){
if(board[row][j] ==i)counter ++;
}
if(counter >1) break; //here you check that there´s only one number on the row.
}
Of course, there are other ways to do it, this one is quite basic and slow.
-------------------------------------EDIT-------------------------------------------
Your function should be bool in order to know if there´s repeated number or not.
bool is_row_ok(vector<vector<int> board)//I suggest you use vectors instead of arrays.
{
//i and j are declared on for, there´s no need to declare them again.
for (int i = 0; i < 10; ++i)
{
int counter = 0;
for (int j = 0; j < 9; ++j)
{
if(board[row][j] == i)counter++;
if(counter > 1)return false;
}
}
return true;
}
Now you call this function on main. If function returns true, everything is ok, if it returns false, you have a repeated number.
int main(){
if(is_row_ok(board)cout<<"No number repeated"<<endl;
else cout << "Number repeated"<<endl;
return 0;
}
I think the easiest way is to use std::sort + std::unique as unique returns iterator to the new end of range.
#include <algorithm>
using namespace std;
int main()
{
int n[15] = { 1, 5, 4, 3, 6, 7, 5, 5, 5, 4, 2, 3, 9, 8, 9 };
int* end = n + 15;
sort(n, n + 15);
bool has_no_duplicates = (unique(n, n + 15) == end);
return 0;
}
This is just an example but you should get the idea.
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int st[20], n,nrSol=0;
void afisare() {
for (int i = 1; i <= n; ++i) {
g << st[i] << " ";
}
g << "\n";
}
int valid(int k) {
for (int i = 1; i < k; ++i) {
if (st[k] == st[i] || abs(st[k]-st[i]) == abs(k-i))
return 0;
}
return 1;
}
void BK(int k) {
for (int i = 1; i <= n; ++i) {
st[k] = i;
if (valid(k)) {
if (k == n) {
++nrSol;
if(nrSol == 1)
afisare();
}
else
BK(k + 1);
}
}
}
int main()
{
f >> n;
BK(1);
g << nrSol << "\n";
return 0;
}
This is the code I have made to solve the N-Queen problem using backtracking , if you do not know the problem , here it is : The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. A queen is attacking another one if they are on the same column, row or on the same diagonal.
If we insert 4, the output will show only the first solution, then the number of solutions on the second row :
2 4 1 3
2
I want to optimise this algorithm using one more array which will store the row the diagonal and the column used by the queen so the complexity of the validation will be O(1), but I do not know how to implement it.
I am a C++ noob. I have a list of numbers that I put into a Vector. All numbers are 9 digit integers and are unique. I want to know what is the least amount of digits (starting from the right) that can be used to uniquily identify each number in the set. right now there are only 6 numbers, but the list could potentially grow into the thousands. I have posted my code thus far (not working.)
EDIT output is the following...
digit is 1
digit is 1
digit is 1
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms
This is mostly a learning exercise. Please be generous and explicit with your comments and solutions.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main() {
//declare stream variable and load vector with values
ifstream myfile("mydata.txt");
vector<int> myVector;
int num;
while (myfile >> num) {
myVector.push_back(num);
}
//sort and squack if there is a duplicate.
std::sort(myVector.begin(), myVector.end());
for (int i = 0; i < (myVector.size() - 1); i++) {
if (myVector.at(i) == myVector.at(i + 1)) {
printf("There are duplicate student numbers in the file");
exit(EXIT_FAILURE);
}
}
//if it get here, then there are no duplicates of student numbers
vector<int> newv;
int k = 1;
bool numberFound = false;
bool myflag = false;
while (numberFound == false) {
//loop through original numbers list and add a digit to newv.
for (int j = 0; j < myVector.size(); ++j) {
newv.push_back(myVector.at(j) % (10^k));
}
sort(newv.begin(), newv.end());
for (int i = 0; i < (newv.size() - 1); i++) {
if (newv.at(i) == newv.at(i + 1)) {
//there is a duplicate for this digit. Set flag.
myflag = true;
}
if (myflag == false) {
numberFound = true;
cout << "digit is " << k << endl;
} else {
k++;
}
}
}
// for (int i = 0; i < myVector.size(); i++) {
// cout << "||" << myVector.at(i) << "||" << endl;
// }
//
// for (int i = 0; i < newv.size(); i++) {
// cout << "---" << newv.at(i) << "---" << endl;
// }
return 0;
}
Check the below code.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
//declare stream variable and load vector with values
ifstream myfile("mydata.txt");
vector<int> myVector;
int num;
while (myfile >> num) {
myVector.push_back(num);
}
//sort and squack if there is a duplicate.
std::sort(myVector.begin(), myVector.end());
for (int i = 0; i < (myVector.size() - 1); i++) {
if (myVector.at(i) == myVector.at(i + 1)) {
printf("There are duplicate student numbers in the file");
exit(EXIT_FAILURE);
}
}
//if it get here, then there are no duplicates of student numbers
vector<int> newv;
int k = 1;
bool numberFound = false;
bool myflag = false;
int p = 1;
while (numberFound == false) {
//loop through original numbers list and add a digit to newv.
newv.clear();
p = p * 10;
for (int j = 0; j < myVector.size(); ++j) {
newv.push_back(myVector[j] % p);
}
sort(newv.begin(), newv.end());
myflag = false;
for (int i = 0; i < (newv.size() - 1); i++) {
if ( newv[i] == newv[i+1]) {
//there is a duplicate for this digit. Set flag.
myflag = true;
break;
}
}
if (myflag == true){
k ++;
}else{
numberFound = true;
cout << "digit is " << k << endl;
break;
}
}
return 0;
}
Sample Input:
123451789
123456687
125456789
123456780
Output:
digit is 4