Can not process the data on file after storing array - c++

I have file that contains 20x20 matrix with random numbers. I've read the numbers on the file and then to store in the array. It seems I had not actually assign the numbers to array, because when I had print one of the number it displayed something like "||" instead of number, see the line cout <<array[0][1]. My complete code is below:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
#define length 20
int main(){
char array[length][length];
char chs;
ifstream grid;
grid.open("D:\\Example\\digit.txt", ios::in);
while (!grid.eof()){
for (int i = 0; i < 19; i++){
for (int j = 0; j < 19; j++){
grid.get(chs);
if (!grid.eof()){
array[i][j] = chs;
cout << setw(1) << array[i][j];///It display the numbers on file, there is no problem here.*/
}
}
}
}
cout <<array[0][2];//There is problem it can not display the number it display something different than numbers.
grid.close();
while (1);
}
Output of the consol, the numbers on the file exactly look like this. cout <<array[0][3] does not print
I've changed the last part
cout << endl;
for (int i = 0; i < 19; i++){
for (int j = 0; j < 19; j++){
cout << array[i][j];
}
}
cout << endl;
grid.close();
Output of the last part that is different than numbers on the file

Your inner loop only runs for the current value of i, meaning that you read no values into the first row, 1 into the second row etc.
for (int i = 0; i < 19; i++){
for (int j = 0; j < i; j++){
// ^ wrong
If you want to read a 20x20 matrix, both your inner and outer loop should run for 20 iterations.
for (int i = 0; i < 19; i++){
for (int j = 0; j < 19; j++){
// ^^
Note also that you probably need to add some code to handle any newlines in your input file. After each set of 20 digits, you'll have one (\n) or two (\r\n) characters indicating a newline. These are a valid part of the text file but presumably aren't required to be stored in your array.

Related

Fence Post Errors When Displaying Arrays

I am currently using C++ on a program called CodeZinger for one of my classes. I was asked to make a program that will output an array with input that the program gives me.
See screenshot below.
The issue is that the program outputs an extra space at the end of my array, which is making the program say that I have not gotten the question right.
#include <iostream>
using namespace std;
int main()
{
int rows = 1;
int cols = 1;
long int arr[100][100];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
My output (see screenshot above) is showing that my code is right and it was going well, except for that extra space at the end. Is there any way to remove that extra space without adding an if statement into my code somewhere?
Have the inner loop iterate until j < cols - 1 and then write one more output line after it ends, without a space (e.g.: std::cout << arr[i][cols-1];) –
UnholySheep

How to check if in arrays column all numbers are negative?

I created an array of A[rows][columns] random numbers between 100 and -100
saved it in txt file
Now i have to check if all numbers are negative in column and if so save column number in array C[x];
int main()
{
ofstream out("masyvas.txt");
srand(time(0));
int n=rand() % (31 - 10) + 10; // eilutes
int m=rand() % (31 - 10) + 10; // stulepliai
int A[n][m];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
A[i][j]=rand()%100 + (rand()%100 *(-1)) ;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
out<<setw(5)<<A[i][j]<<" ";
out<<endl;
}
out.close();
//------------------------------------------
return 0;
}
so above everything is ok but
ofstream rez("rez.txt");
for(int i=0; i < m;i++)
{
for(int j=0; j< n;j++) {
if(A[i][j]<0){
rez << i <<" - column" << j << "- row"<< endl;
}
}
}
cout << n << endl << m << endl;
rez.close();
first i wanted to print into txt file if it finds negative numbers correctly but i get some nonsense
and i have no clue what to do im stuck for half an hour trying to do different things
First thing, don't panic, read your code and actually know what it is doing.
To check if all numbers in a column are negative, you need to loop over an entire column first while having something, preferably a boolean, to tell you if any of them is not negative.
For example: create a flag that defaults to true.
bool allNegative = true;
for ( int i =0; i < (sizeof(matrix[0])/sizeof(int)); i++)
{
if(matrix[i][0] > 0) allNegative = false; // change the flag to indicate that it is not all negative
}
Then what you want to do is if the flag is still true, loop that column again to just copy and paste all this code for each row to output that column to a txt file.
out<<setw(5)<<A[i][j]<<" ";
out<<endl;
EDIT: thanks to comment, I have changed the syntax error.

Debugging this code to find the highest product of 13 adjacent digits in a 1000 digit number

I have tried to create a C++ program to solve Project Euler's 8th problem. However, my program is not outputting the correct answer. I have tried to trace through it, but I can't locate my logical error.
Project Euler's 8th problem is about finding the value of the highest product of thirteen adjacent digits in a number with 1000 digits.
My code outputs 9223372019204292608 as the answer. The correct answer is I believe 23514624000.
My code is the following :
#include <iostream>
#include <cassert>
using namespace std;
int main() {
//Declare input number as a string. Declare variables
string input = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
char initialArray[1000];
int finalArray[1000];
long products[987];
long highestProduct = 0L;
//Divide input string into an array of single characters
for(int i = 0; i < 1000 ; i++){
initialArray[i] = input.at(i);
}
//Translate the array of characters into an array of single-digit integers
for(int i = 0; i < 1000 ; i++){
finalArray[i] = input.at(i) - '0';
}
//Initialize each element in the products array to 1
for (int i = 0; i < 100 ; i++){
products[i] = 1L;
}
//Perform multiplications
for(int i = 0; i < 987; i++){
for( int j = 0; j < 13; j++){
products[i] = products[i] * finalArray[j+i];
}
}
for(int i = 0; i < 987; i++){
if (products[i] > highestProduct){
highestProduct = products[i];
}
}
cout << "The highest product of 13 adjacent digits from the input is: " << highestProduct << endl;
return 0;
}
//Correct answer is supposed to be 23514624000
You are not initializing all of the products array. Ideally you shouldn't be initializing them separately at all.
//Perform multiplications
for(int i = 0; i < 987; i++)
{
products[i] = 1L; // initialize
for( int j = 0; j < 13; j++)
{
products[i] = products[i] * finalArray[j+i];
}
}
This way, you can't go wrong with initializing all the elements.
Look here:
//Initialize each element in the products array to 1
for (int i = 0; i < 100 ; i++){
Also note you can use the only loop through array using update of product at every step like this:
product = (product / finalArray[i - 13]) * finalArray[i]

How do you calculate the total number of -1s and the total numbers of 1s in the table? (c++)

Here is the coding below :) I have also commented on some parts so that it is easier to understand the output of the code.
I have a slight idea that I need to use an "if statement" with "rand()%" in order to make sure that the program knows we want to calculate the sum of 1s and -1s only. for e.g using "rand()%2-1" can help with getting the total sum of 1s outputted in the table. Again, I'm not sure if this idea will work or not.
So the program should output something like "The amount of 1s in the table is 5 and the amount of -1s in the table is 3" for the first time its ran. Then when it is ran the second time, it could output something like "The amount of 1s in the table is 2 and the amount is -1s in the table is 5"
Sorry for any confusions and All your help will be highly appreciated :) :)
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;
int table[ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++)
cout << setw(3) << table[i][j];
cout << endl;
}
bool checkTable [ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
if (table[i][j] !=0) {
checkTable[i][j] = true;
}
else{
checkTable[i][j] = false;
}
//The output in the line below is the final line outputted by the
console. This prints out "1" if there is a value in the index within
the table provided above (the value is represented by 1 or -1), and
prints out "0" if there is no value in the index (represented by 0)
cout << " " << checkTable[i][j];
}
}
return 0;
}
[...] for e.g using "rand()%2-1" can help with getting the total sum of 1s
outputted in the table.
I dont really understand what you mean by that. Counting and randomness dont go well together. I mean of course you can fill a matrix with random numbers and then do some counting, but rand() wont help anything for the counting.
You need something as simple as that:
int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;
int table[ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}
unsigned ones_counter = 0;
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) { // dont forget the bracket
cout << setw(3) << table[i][j];
if (table[i][j] == 1) { ones_counter++;} // <- this is counting
}
cout << endl;
}
std::cout << "number of 1s in the table : " << ones_counter << "\n";
....

Decrypting strings with arrays?

I'm having trouble with one of my c++ assignments.
It's about decrypting a string of letters.
Here is a picture my teacher sketched up:
https://gyazo.com/33d90496958ef231dec7866e39ce1951
I must insert a string of letters using the command line. See the letters to the left on the picture i linked. They will be inserted in an array and it must this message: "DETTA ÄR KYPTERAT". It's in swedish, and it translates to "THIS IS ENCRYPTED".
The thing I'm having most trouble with is inserting the text into the multidimensional array using CIN.
It HAS to be a CIN in the beginning. Please answer in a simple and understandable as i'm still pretty novice at C++!
Without going into the details of your encryption algorithm, filling the 2d-array from standard input can be as follows:
int arr[ROWS][COLS] = {0};
char c;
for(int j = 0; j < COLS; j++)
{
for(int i = 0; i < ROWS; i++)
{
cin.get(c);
arr[i][j] = c;
}
}
// just output for testing
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
I suppose int type will be good for your algorithm, but of course it is just example, and you can do any changes.