Putting String into a 2D Matrix in Objective C++ - c++

So I'm using Objective C++ and I want to put a string into a 4 by X (X = length of string/4) int array by using the ASCII code. The first quarter of the string (which is formatted to fit completely into a 4 by X array) is supposed to go in [0][col], the second quarter into [1][col], the third quarter into [2][col] and the fourth quarter into [3][col]. So I tried the following with 4 for loops, but it doesnt work at all, and I just can't seem to get it to work somehow. Any suggestions would be greatly appreciated.
textMatrix is the matrix in which I want to put the NSString/ASCII number, and inputFinal is the NSString itself. Length * (1/4) or whatever is also always going to be an integer.
for(int i = 0; i < length*(1/4); i++)
{
textMatrix[0][i] = (int)[inputFinal characterAtIndex: i];
}
for(int j = length*(1/4); j < length*(2/4); j++)
{
textMatrix[1][j] = (int)[inputFinal characterAtIndex: j];
}
for(int k = length*(2/4); k < length*(3/4); k++)
{
textMatrix[2][k] = (int)[inputFinal characterAtIndex: k];
}
for(int l = length*(3/4); l < length; l++)
{
textMatrix[3][l] = (int)[inputFinal characterAtIndex: l];
}

You can rewrite your 4 loops in 1 loop:
for(int i = 0; i < length; i++)
{
textMatrix[i/4][i%4] = (int)[inputFinal characterAtIndex:i];
}

I don't think I understand what you're trying to do..
Given a string: "Here";
do you want:
Matrix[0][0] = 'H';
Matrix[1][1] = 'e';
Matrix[2][2] = 'r';
Matrix[3][3] = 'e';
If so then this works:
#import <objc/objc.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>
#implementation TestObj
int main()
{
NSString* str = #"Here";
int matrix[4][4] = {0};
for (int i = 0, j = 0; j < 4; ++j)
{
matrix[i][i++] = (int) [str characterAtIndex: j];
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("%c", (char)matrix[i][j]);
}
}
return 0;
}
#end
The above prints Here.

actually a double loop like so ended up working best for me:
int index = 0;
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < length/4; col++)
{
textMatrix[row][col] = (int)[inputFinal characterAtIndex:index];
index++;
}
}

Related

Return matrix from function with variable lenght

I want to return the matrix from the function, but I can't find a way how. I've found some ways, but they can't be used for VLA. I've read about using std::vector, but that also didn't work.
int gengrid(int gridsize)
{
gridsize = 10 - 1;
int grid[gridsize+3][gridsize+3];
srand(time(NULL));
int count = 0;
std::fill_n(grid[0], 12, 0);
for(int i = 1; i < gridsize + 2; i++)
{
grid[i][0] = 0;
for(int j = 1; j < gridsize + 2; j++)
{
grid[i][j] = rand()%2;
}
grid[i][gridsize+2] = 0;
}
std::fill_n(grid[gridsize+2], gridsize + 3, 0);
return grid;
}
Okay, I found out my solution.
I initialize vector matrix with
static std::vector<std::vector<int>> grid(gridsize+3, std::vector<int>(gridsize+3));
which sets 0 by default for all elements.
(honestly, I don't know, how it's working, maybe somebody would comment explanation of this behavior.)
Complete code here:
#include <iostream>
#include <time.h>
#include <vector>
std::vector<std::vector<int>> gengrid(int gridsize)
{
gridsize = 10 - 1;
static std::vector<std::vector<int>> grid(gridsize+3, std::vector<int>(gridsize+3));//[gridsize+3][gridsize+3];
srand(time(NULL));
int count = 0;
for(int i = 1; i < gridsize + 2; i++)
{
grid[i][0] = 0;
for(int j = 1; j < gridsize + 2; j++)
{
grid[i][j] = rand()%2;
}
grid[i][gridsize+2] = 0;
}
return grid;
}
int main()
{
std::vector<std::vector<int>> grid = gengrid(10);
for(int i = 0; i < 9 + 3; i++)
{
for(int j = 0; j < 9 + 3; j++)
{
std::cout << grid[i][j];
}
std::cout << std::endl;
}
return 0;
}

Counting sort not sorting correctly

I have written this counting sort algorithm, but am not sure why it isn't working... Could anyone check and give me a few pointers on what to fix? Thanks!
#include <iostream>
using namespace std;
int main(){
int arr[10] = {1434, 1415, 1217, 4218, 3618, 176, 1021, 3785, 1891, 1522};
int C[4219];
for (int i = 0; i < 4219; ++i) {
C[i] = 0;
}
for (int j = 0; j < 10; ++j) {
C[arr[j]] = C[arr[j]] + 1;
}
for (int k = 10; k > 0; --k) {
C[k] = C[k] + C[k + 1];
}
int B[10];
for (int l = 0; l < 10; ++l) {
B[C[arr[l]] - 1] = arr[l];
C[arr[l]] = C[arr[l]] - 1;
}
for (int m = 0; m < 10; ++m) {
cout << B[m] << " ";
}
return 0;
}
The problem is in the third loop. You iterate only through 10 elements of the array C.
You had created small mistake in the code.....
#include <iostream>
using namespace std;
int main(){
int arr[10] = {1434, 1415, 1217, 4218, 3618, 176, 1021, 3785, 1891, 1522};
int C[4219];
for (int i = 0; i < 4219; ++i) {
C[i] = 0;
}
for (int j = 0; j < 10; ++j) {
C[arr[j]] = C[arr[j]] + 1;
}
for (int k = 1; k < 4219; ++k) { // mistake
C[k] = C[k] + C[k - 1];
}
int B[10];
for (int l = 9; l >=0; --l) { // suggestion
B[C[arr[l]] - 1] = arr[l];
C[arr[l]] = C[arr[l]] - 1;
}
for (int m = 0; m < 10; ++m) {
cout << B[m] << " ";
}
return 0;
}
Beside that I would like to give you one suggestion that in the loop traverse from right to left as it will maintain the stability of the sort..
Stability means suppose if array has two or more same element then in the stable sort,element which is before in unsorted array will occur first in sorted array.

how to improve performance of 2d array in C++

I have a low-level function that will be called millions of times, so it should be very efficient. When I use "gprof" in Linux, I found that a part of the code takes 60% of the total computation of the function (the rest part is to solve the roots of a cubic equation). Here Point is a data structure has x and v, which will be converted to a matrix for later use. The idea is to subtract each row by the first row. The code shows like below
double x[4][3] = {0}, v[4][3] = {0};
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = Point[i]->v[j];
x[i][j] = Point[i]->x[j];
}
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = v[0][j] - v[i][j];
x[i][j] = x[0][j] - x[i][j];
}
}
Can anyone show me the problem of this code? Why it performs so badly?
You can do it all in one pass:
double x[4][3] = {
{ Point[0]->x[0], Point[0]->x[1], Point[0]->x[2] }
};
double v[4][3] = {
{ Point[0]->v[0], Point[0]->v[1], Point[0]->v[2] }
};
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] = x[0][j] - Point[i]->x[j];
v[i][j] = v[0][j] - Point[i]->v[j];
}
}
You could even take that to the next level and put the entire thing into the initializers for x and v.
Or, if x and v in Point are each contiguous arrays:
double x[4][3], v[4][3]; // no init
// fill entire arrays
for (int i = 0; i < 4; ++i){
memcpy(x[0], Point[0]->x, sizeof(x[0]));
memcpy(v[0], Point[0]->v, sizeof(v[0]));
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] -= Point[i]->x[j];
v[i][j] -= Point[i]->v[j];
}
}

Comparing pixels in alpha-trimmed filter

I have the following problem. I have written code for alpha-trimmed filter in opencv library. I think that it is properly constructed but I don't know how to compare two 3 channels pixels during sorting a 'window with pixels'. In my code it is done but comparing two but it is impossible for vectors. I assume that i should compare it one channel and after second and so on. Have you any hints for me, or could you propose some modifications in my code. This is my code.
int alphatrimmed(Mat img, int alpha)
{
Mat img9 = img.clone();
const int start = alpha;
const int end = 9 - alpha;
//going through whole image
for (int i = 1; i < img.rows - 1; i++)
for (int j = 1; j < img.cols-1; j++)
{
int k = 0;
Vec3b element[9];
//selecting elements
for (int m = i - 1; m < i + 2; m++)
for (int n = j - 1; n < j + 2; n++)
element[k++] = img.at<Vec3b>(m*img.cols + n);
for (int i = 0; i < end; i++)
{
int min = i;
for (int j = i + 1; j < 9; j++)
if (element[j] < element[min])
min = j;
Vec3b temp = element[i];
element[i] = element[min];
element[min] = temp;
}
const int result = (i - 1)*(img.cols - 2) + j - 1;
img9.at<Vec3b>(result) = element[start];
for (int j = start + 1; j < end; j++)
img9.at<Vec3b>(result) += element[j];
img9.at<Vec3b>(result) /= 9 - alpha;
}
namedWindow("AlphaTrimmed Filter", WINDOW_AUTOSIZE);
imshow("AlphaTrimmed Filter", img9);
return 0;
}
Thank you for your time spent on solving my problem.

How do I create a 2d array pointer with my own class as type?

I am trying to create a 2d array pointer with my own class, Tile, as type. I have looked at the code example at How do I declare a 2d array in C++ using new?. The following code works perfectly:
int** ary = new int*[sizeX];
for(int i = 0; i < sizeX; ++i)
ary[i] = new int[sizeY];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
ary[i][j] = 5;
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
cout << ary[i][j];
However when I try to change type from int to my own class, Tile, I get an
No viable overloaded '='
error in XCode, and I can't figure out what this means. I use the following code:
Tile** t;
t = new Tile*[8];
for(int i = 0; i < 8; ++i)
t[i] = new Tile[8];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
t[i][j] = new Tile(new NoPiece());
}
}
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << (t[i][j].get_piece()).to_string();
}
}
Here is the code for Tile.cpp:
#include "Tile.h"
Tile::Tile() {
}
Tile::Tile(Piece p) {
piece = &p;
}
Piece Tile::get_piece() {
return *piece;
}
And the code for Tile.h:
#include <iostream>
#include "Piece.h"
class Tile {
Piece * piece;
public:
Tile();
Tile(Piece p);
Piece get_piece();
};
The difference between two code snippets is that the one using int treats array elements like values, i.e. assigns
ary[i][j] = 5;
while the one using Tile treats array elements like pointers:
t[i][j] = new Tile(new NoPiece()); // new makes a pointer to Tile
Change the assignment to one without new to fix the problem:
t[i][j] = Tile(new NoPiece());
There is nothing wrong to making a 2D array of pointers, too - all you need is to declare it as a "triple pointer", and add an extra level of indirection:
Tile*** t;
t = new Tile**[8];
for(int i = 0; i < 8; ++i)
t[i] = new Tile*[8];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
t[i][j] = new Tile(new NoPiece());
}
}
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << (t[i][j]->get_piece()).to_string();
}
}
// Don't forget to free the tiles and the array
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
delete t[i][j];
}
delete[] t[i];
}