Can't load images insade array in sfml - sfml

I am new in SFML and working on game "Memory Match" 4x4. To simplify I am loading 2 different image.Each time I get the last image on array.Can anyone say me what I am doing wrong with this?
string s[] = {
"C:/Users/Eldar/Desktop/sfml/image.png",
"C:/Users/Eldar/Desktop/sfml/redDie.png"
};
int k = 0;
for (int i = 0; i <= 3; i++){
for (int j = 0; j <= 3; j++){
cout << k << endl;
Cards[i][j].setScale(1, 1);
Cards[i][j].setPosition(0 + ioffset, 0 + joffset);
textureCard.loadFromFile(s[k]);
Cards[i][j].setTexture(textureCard);
k++;
if (k == 2) k = 0;
}
}

Related

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.

image made from dots and letters c++

very basic Q as I just started with coding but I stuck at some point and have 0 ideas what to do.
I need to write code to get diamond shape made from dots and X letters, size based on a value (n) provided by the user, (3 ≤ n ≤ 80).
for example:
As I mentioned - I have almost 0 experience so all I could get is is this shape for n=6
height is ok, same as widht but unfortunately, the amount of X's and placement isn't correct :/
my code:
int h;
cerr << "Provide size of diamond: ";
cin >> h;
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= h-i ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
cout << endl;
Thank you all good people who will help mi with this one :)
I help to draw points. I hope you, looking on my change, are able to update your code further to achive the required picture.
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
cout << endl;
}
In this kind of problems you can divide the problems in different parts. Such as for n=6 the image can be divided in 4 mirror images:
..X
.XX
XXX
then,
X..
XX.
XXX
and upside down mirror of them.
You said that you can draw the first one. I think if you give some more time you will be able to print the full image too.
But, if you have problems, here's the code for that
for (int i = 1; i <= h; i++) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
for (int i = (h/2)*2; i > 0; i--) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
Since this is tagged as a c++ question let's use std::string and three loops.
#include <iostream>
#include <string>
void print_diamond(int n)
{
int np = n / 2, nm = (n - 1) / 2;
int npl = np, nml = nm;
std::string str(n, '.');
for (int i = 0; i < nm; i++)
{
str[npl++] = 'X'; str[nml--] = 'X';
std::cout << str << std::endl;
}
for (int i = nm; i <= np; i++)
{
str[npl] = 'X'; str[nml] = 'X';
std::cout << str << std::endl;
}
for (int i = np; i < n - 1; i++)
{
str[npl--] = '.'; str[nml++] = '.';
std::cout << str << std::endl;
}
std::cout << std::endl;
}
Print all diamond for a shinier world...
int main()
{
for (int n = 3; n < 81; n++)
{
print_diamond(n);
}
}

dynamically allocating memory to matrices

I am trying to convert an array to a matrix, so I dynamically allocated memory to the matrix, but I'm getting an error:
CRT detected that the application wrote to memory after end of heap buffer
int main() {
float a[9] = { 1, 3, 5, 6,4,6,5,6,8};
int b = sizeof(a)/sizeof(a[0]);
int r = sqrt(b) - 1;
float **A_mat = new float*[r];
//A_mat = ;
for (int i = 0; i <= r; i++)
A_mat[i] = new float[r];
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)];
}
}
cout << "a[0,0] is " << A_mat[0][0] << endl;
cout << "a[0,2] is " << A_mat[0][2] << endl;
cout << "a[1,0] is " << A_mat[1][0] << endl;
cout << "a[2,0] is " << A_mat[2][0] << endl;
for (int i = 0; i <= r; i++) {
delete[] A_mat[i];
}
delete[] A_mat;
system("pause");
}
Your code has two major issues:
Issue 1:
float **A_mat = new float*[r]; // allocating memory for 2 rows because r = sqrt(9) - 1 => r = 2
for (int i = 0; i <= r; i++)
A_mat[i] = new float[r]; // Usage of A_mat[r] for r = 2 is out of bounds. Also, memory is allocated for 2 columns.
Issue 2:
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)]; // accessing indices (0, 2), (1, 2), (2, 0), (2, 1), (2, 2) are out of bounds
}
}
Instead, you should allocate (r + 1) size memory. Like this:
int main() {
float a[9] = { 1, 3, 5, 6,4,6,5,6,8};
int b = sizeof(a)/sizeof(a[0]);
int r = sqrt(b) - 1;
float **A_mat = new float*[r + 1];
//A_mat = ;
for (int i = 0; i <= r; i++)
A_mat[i] = new float[r + 1];
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)];
}
}
cout << "a[0,0] is " << A_mat[0][0] << endl;
cout << "a[0,2] is " << A_mat[0][2] << endl;
cout << "a[1,0] is " << A_mat[1][0] << endl;
cout << "a[2,0] is " << A_mat[2][0] << endl;
for (int i = 0; i <= r; i++) {
delete[] A_mat[i];
}
delete[] A_mat;
system("pause");
}
Suggestions:
1) Never use raw pointers. Always look for safer alternatives. In this case, you should go for std::vector.
2) Never use naked new. It is generally the major source of Memory leaks. Though currently, its not the case for you. But it might become an issue in the future.
3) Always remember that array starts from 0-indexing in C++.
You have problem with for loop. The index is from 0 to (r-1). But your for loop from 0 to r. It made out of bound of your array.
1> So please replace this for loop
for (int i = 0; i <= r; i++)
by
for (int i = 0; i < r; i++)
2> And replace this statements
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)];
}
}
by
for (int j = 0; j < r; j++) {
for (int i = 0; i < r; i++) {
A_mat[j][i] = a[i + j * r];
}
}

Trouble with sorting 2d double array alongside 1d string array.

This is a working program that I've been trying to learn C++ from that inputs from a file strings and doubles and puts them into their perspective arrays. What I'm stuck on is while sorting the strings I want to sort the array of doubles in ascending order and keep the scores with the name they are associated with. I'm trying to learn it without using vectors before I go on to learn how to manipulate them, that's the reason for not using vectors before anyone asks.
Is it best to sort the columns of the 2d array and than sort them with the 1d array or just to have a statement that does it all? Also what would be the best sorting algorithm for this application?
My attempts have all failed so far so I turn to the community here for help. The logic is most likely some simple concept that I have yet to grasp. We all have to start somewhere. Thank you in advance for your help.
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void ArraySort (string x[], double y[][3], int length);
int main()
{
ifstream inFile;
inFile.open("bowlers2.txt");
const int SIZE = 10;
int i,j;
double scores[10][3];
string names[SIZE];
string mystring;
if (!inFile)
{
cout << "Can not open the input file"
<< " This program will end."<< "\n";
return 1;
}
for(i = 0; i < SIZE; i++)
{
getline(inFile, names[i]);
for(j = 0; j < 3; j++)
{
getline(inFile, mystring);
scores[i][j] = atoi(mystring.c_str());
}
}
for(int i=0;i<SIZE;i++)
{
cout << names[i] << "\n";
for(j = 0; j < 3; j++)
cout << scores[i][j] << "\n";
}
inFile.close();
ArraySort (names, scores, SIZE);
return 0;
}
void ArraySort (string x[], double y[][3], int LENGTH)
{
int i,j;
string sValue;
double dValue;
for(i = 1; i < LENGTH; i++)
{
sValue = x[i];
for(j = i - 1; j >= 0 && x[j] > sValue; j--)
{
x[j + 1] = x[j];
}
x[j + 1] = sValue;
}
cout << "\n";
for(int i=0;i<LENGTH;i++)
{
cout << x[i] << "\n";
for(j = 0; j < 3; j++)
cout << y[i][j] << "\n";
}
}
file read by program:
Linus too good
100
23
210
Charlie brown
1
2
12
Snoopy
300
300
100
Peperment Patty
223
300
221
Pig Pen
234
123
212
Red Headed Girl
123
222
111
Marcey
1
2
3
keith hallmark
222
300
180
anna hallmark
222
111
211
roxie hallmark
100
100
2
I think what you’re trying to do is this:
void ArraySort (string x[], double y[][3], int LENGTH)
{
int i,j,k;
string sValue;
double dValue;
double dArray[3];
for(i = 1; i < LENGTH; i++)
{
sValue = x[i];
for (k = 0; k < 3; k++)
{
dArray[k] = y[i][k];
}
for(j = i - 1; j >= 0 && x[j] > sValue; j--)
{
x[j + 1] = x[j];
for (k = 0; k < 3; k++)
{
y[j + 1][k] = y[j][k];
}
}
x[j + 1] = sValue;
for (k = 0; k < 3; k++)
{
y[j + 1][k] = dArray[k];
}
}
for(k = 0; k < LENGTH; k++)
for(i = 1; i < 3; i++)
{
dValue = y[k][i];
for(j = i - 1; j >= 0 && y[k][j] > dValue; j--)
{
y[k][j + 1] = y[k][j];
}
y[k][j + 1] = dValue;
}
}
cout << "\n";
for(int i=0;i<LENGTH;i++)
{
cout << x[i] << "\n";
for(j = 0; j < 3; j++)
cout << y[i][j] << "\n";
}
}
For learning purposes you can use bubble sort. Bubble sort is very easy, also very inefficient and slow. In a real application you would use std::sort
template <typename T>
void bubble_sort(T *num, int num_count)
{
for (int i = 0; i < (num_count - 1); i++)
for (int j = i + 1; j < num_count; j++)
if (num[i] > num[j])
std::swap(num[i], num[j]);
}
void ArraySort(string str[], double num[][3], int str_count)
{
int num_count = 3;
bubble_sort(str, str_count);
for (int i = 0; i < str_count; i++)
bubble_sort(num[i], num_count);
cout << "\n";
for (int i = 0; i < str_count; i++)
{
cout << str[i] << "\n";
for (int j = 0; j < num_count; j++)
cout << num[i][j] << "\n";
}
}

Putting String into a 2D Matrix in Objective 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++;
}
}