I would like assistance on how I can go about a project I was assigned in which a matrix of letters is printed and user-defined words are located in all directions on the grid and colored red using colormod.h and the matrix files he provides us with. For example, executing "a.exe hard project < 0505matrix" will print the assigned inputs of the matrix with the words "hard" and "project" in red text wherever located. I'm currently expanding on my previous project (shown below) in which one word was searched with specific coordinates to search given that returns "true" or "false" and I believe my first step would be to use nested for loops in some manner to allow the word to be searched WITHOUT the given coordinates. Any assistance would be greatly appreciated! Thank you
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
bool search(vector< vector<char> > &matr, char *word, int x, int y){
bool answer = false;
if(strlen(word) > (matr.size()-y)){
return false;
}
else{
for(int i = 0; i < strlen(word); i++){
if(word[i] == matr[x][y + i])
answer = true;
else
answer = false;
break;
}
}
return answer;
}
int main(int argc, char *argv[]){
int x, y;
string word;
cin >> x;
cin >> y;
vector< vector<char> > matr;
matr.resize(x);
for(int i = 0; i < matr.size(); i++){
matr[i].resize(y);
}
for (int i = 0; i < x; i++) {
for (int j = 0; j<y ; j++){
cin >> matr[i][j];
}
}
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++){
cout << matr[i][j] << " ";
}
cout << endl;
}
cin >> word;
cout << word;
bool check = search(matr, argv[1], atoi(argv[2]), atoi(argv[3]));
if(check){
cout << "true" << endl;
}
else{
cout << "false" << endl;
}
}
Related
As you can see from the title I need to decrypt the strings in a text file. I have major problems with this so if you can help me I would really appreciate it.
First of all, here is the input file:
saoreecessinntfi
pmrrj ie2
borj
I want to decrypt these words like this:
sesnaestocifreni
primjer 2
broj
I have used the matrix 4x4 to do this, and here is the code so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream test;
test.open("test.txt");
char word[5][5];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
test >> word[i][j];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << word[j][i];
}
}
return 0;
}
Here is the output:
sesnaestocifreni
It only outputs the first word in text file. I think the problem with this is that I do not know how "long" is "i" and "j" in those other words beacuse the first word has 16 charachters so the counter "i" and "j" are set on 4. How to count each words charachters and if they are the same then decrpyt the word. Also if the word is right spelled I need to cout in the program "ERROR". For example
apple
I do not need to decrypt this word, beacuse it is right word, and "i" and "j" would not be the same or I do not know what I am talking about.
I think this should work just the fine for your case:
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
int matrixSize(std::string &str) {
auto x = sqrt(str.length());
return x - floor(x) == 0 ? x : 0;
}
int main() {
std::fstream file("test.txt");
std::string str;
while (std::getline(file, str)) {
if (int n = matrixSize(str)) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
std::cout << str.at(j * n + i);
std::cout << std::endl;
} else
std::cout << "ERROR" << std::endl;
}
return 0;
}
Sample test.txt file:
saoreecessinntfi
pmrrj ie2
borj
apple
Output on test run:
sesnaestocifreni
primjer 2
broj
ERROR
If I understand your problem correctly, you are given a line of n*n characters and need to unscramble it as given.
while (true) {
std::string line;
std::getline(cin, line);
if (line.empty())
break;
int n = 1;
while (n*n < line.size()) {
n++;
}
if (n*n != line.size()) {
std::cout << "ERROR" << std::endl;
continue;
}
std::string unscrambled;
for (int col = 0; col < n; col++)
for (int row = 0; row < n; row++)
unscrambled.append(1, line[row * n + col]);
std::cout << unscrambled << std::endl;
}
I have coded this program and it works fine. I get the result I want but because we are using an old system to submit it, my code is rejected because it saying that the 3 last lines generate a blank of my code. Can someone please tell me where is the problem and how I can fix it? Thank you!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(i = 1; i <= row_nr; i++)
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
You're outputting a space after every value, so there is going to be a space at the end of each line. You should add a check so that you don't output a space after the last value of each line. It seems like you might have intended to do this, but forgot to write the if statement.
#include <iostream>
//#include <iomanip> why?
using namespace std;
int main()
{
int row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(int i = 1; i <= row_nr; i++) //declare iterator variable in for loop statement
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
if(j < row_nr - 1) //you forgot this line
{
cout << " ";
}
}
cout << '\n'; //endl flushes the buffer, unnecessary here
}
return 0;
}
When i run this program (i am using codeblock and its fully upgraded), it shows a box with:
''''.exe has stopped working
A problem caused the program to stop working correctly. Windows will close the program and notify if a solution is available.''''
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
int main()
{
int no, hlf, arr[no], arrno;
cout << "ENTER A NUMBER";
cin >> no;
hlf = ceil(no/2);
for(int i = 1;i <= no;i++)
{
for(int j = 2;j <= hlf;j++)
{
int ij = i/j;
if(j != i && ij == 0)
{
goto cont;
}
else
{
continue;
}
}
arr[arrno] = i;
arrno++;
cont: ;
}
for(int k = 0;k <= arrno;k++)
{
cout << arr[k] << " ";
}
getch();
return 0;
}
There are few mistakes in your code
no need of #include <conio.h> and getch();
Array arr[no] declaration is wrong. It should be int arr[50];
Here is the corrected code that runs fine.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int no, hlf, arrno;
int arr[50];
cout << "ENTER A NUMBER";
cin >> no;
hlf = ceil(no/2);
for(int i = 1;i <= no;i++)
{
for(int j = 2;j <= hlf;j++)
{
int ij = i/j;
if(j != i && ij == 0)
{
goto cont;
}
else
{
continue;
}
}
arr[arrno] = i;
arrno++;
cont: ;
}
for(int k = 0;k <= arrno;k++)
{
cout << arr[k] << " ";
}
return 0;
}
thanks guys, i got the answer. it was my bad, i didn't post that i need to print prime numbers. its my first question in a web forum. never used one.
ps -> thanks again
include
include
using namespace std;
int main()
{
int numb = 12, half;
int arra[50], arrno = 0;
half = ceil(numb/2);
for(int r = 2;r <= numb;r++)
{
for(int t = 2;t <= half;t++)
{
if(r%t != 0 || t == r) continue;
else goto rpp;
}
arra[arrno] = r;
arrno++;
continue;
rpp:
continue;
}
for (int v = 0;v < arrno;v++)
{
cout << arra[v] << " ";
}
return 0;
}
I'm trying to sort my array pairs by int, but my sort is saying 'unable to resolve identifier' to pairs.begin(), pairs.end(), and compare_pairs_second(). I cannot figure out why but i'm probably doing something wrong?
Here is my code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
main()
{
string name[10];//declaring an array name
int number[10];//declaring an array number
cout << "Please input 10 names \n";//output
for(int i = 0; i < 10; i++){//for statement
cin >> name[i];//inputting names in the array names
}
cout << "Please input their corresponding numbers \n";//output
for(int x = 0; x < 10; x++){
cin >> number[x];//inputting numbers
}//end for
int i = 0;//redeclaring i to be 0
int x = 0;//redeclaring x to be 0
for(int l = 0; l < 10; l++)//for statement
{
cout << name[i] << ": " << number[x] << "\n";
i++;//adding 1 to i so outputs all of array name
x++;//adding 1 to x so outputs all of array number
}//end for
pair<string, int> pairs[10];
int i = 0;
int x = 0;
for(int z = 0; z < 10; z++)
{
pairs[z] = make_pair(name[i], number[x]);
i++;
x++;
}
std::sort(pairs.begin(), pairs.end(), compare_pairs_second<std::less>());
int i = 0;
int x = 0;
for(int z = 0; z < 10; z++)
{
name[i] = pairs[z].first;
number[x] = pairs[z].second;
i++;
x++;
}
string search = "";
cout << "Enter a name to search for";
cin >> search;
size_t found = pairs.find(search);
if(found!=string::npos)
cout << search << pairs;
} //end main
UPDATE/EDIT:
I got my sort to work...to an extent. I deleted the line of code that was giving me errors, and it now sorts my number array, which is half of what I wanted. But now how do I keep the names with their respective numbers without using my pairs variable that I had prior to this fix?
Ex
array number[5]={5, 2, 9, 11, 27};
array name[5]={"Steve", "John", "Bob", "Larry", "Patric"};
Output after sorting:
John: 2 Steve: 5 Bob: 9 Larry: 11 Patric: 27
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
main()
{
const int size = 10;
string name[10];//declaring an array name
int number[10];//declaring an array number
cout << "Please input 10 names \n";//output
for(int i = 0; i < 10; i++){//for statement
cin >> name[i];//inputting names in the array names
}
cout << "Please input their corresponding numbers \n";//output
for(int i = 0; i < 10; i++){
cin >> number[i];//inputting numbers
}//end for
sort(number,number+size);
int i;
int j;
int min;
int counter = 0;
for(i = 0; i < counter; i++)
{
min = i;
for(j = i+1; j < counter; j++)
{
if(name[j] < name[min])
{
string tempString = name[i];
name[i] = name[j];
name[j] = tempString;
int tempInt = number[i];
number[i] = number[j];
number[j] = tempInt;
}
}
}
for(i = 0; i < 10; i++)
{
cout << name[i] << ": " << number[i] << "\n";
};
} //end main
Current output sorts numbers but does not keep names with them.
pairs is raw array, not a STL container; you can't invoke method on it like pairs.begin() and pairs.end().
Since C++11 you could use std::begin() and std::end(), which are overloaded for supporting raw arrays. e.g.
std::sort(std::begin(pairs), std::end(pairs), compare_pairs_second<std::less>());
I am new to C++ I am making an array based off of user input and displaying whether the array is unique or not. my initial thought is I have to end up creating another array to store values and then compare the elements. My code compiles without errors but does not do what I want it to do. I can't used advanced methods such as hashmaps or vectors. Any help or insight is greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int myArray[6];
string name;
int i, k;
int ov = 0;
int newVal = 0;
for (int index = 0; index < 6; index++)
{
cout << "Enter number a number: ";
cin >> myArray[index];
}//end loop for
for (i = 0; i < 6; i++)
{
ov = myArray[i];
}
for (k = i + 1; k < 6; k++)
{
if (ov == myArray[k])
{
newVal = 1;
}
}
if (newVal == 1)
{
cout << "Not all unique";
}
else
{
cout << "All unique";
}
cin.get();
return 0;
}