i need to make a function that take an int matrix of known columns but varies rows which will be used later in sorting and checking if the matrix is row-magic/column magic I know how to do the sorting, and everything else but my issue is setting the end of my loops because they could be either 4/5/6
Note: I am a student so they don't expect me to use sizeof
int sort(int A[? ][5]) {
int i, j, temp;
for (i = 0; i < ? ; i++) { // the number of rows
int min = A[i][0];
for (j = 0; j < 5; j++) { //the number of columns
temp = A[i][j];
if (min > temp) {
int swap = min;
min = temp;
temp = swap;
}
}
int swap = A[i + 1][0];
if (swap < A[i][5]) {
int swap1 = A[i][5];
swap = A[i][5];
swap1 = A[i + 1][0;]
}
}
}
The array A decays to a pointer when it's passed to the function and a pointer doesn't contain any information about the size. You have to pass the number of rows as additional function argument:
int sort(int A[][5], int rows)
Related
Basically, I have written a program to calculate the determinant of a matrix.
However, this feels like quite static yet (i.e. the dimension is passed as an argument). Is there any way to make it more dynamic (without vectors) with something like pointers?
#include <bits/stdc++.h>
using namespace std;
#define N 4
void getCofactor(int mat[N][N], int temp[N][N], int p, int q, int n)
{
int i = 0, j = 0;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
if (row != p && col != q)
{
temp[i][j++] = mat[row][col];
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
int determinantOfMatrix(int mat[N][N], int n)
{
int D = 0;
if (n == 1)
return mat[0][0];
int temp[N][N];
int sign = 1;
for (int f = 0; f < n; f++)
{
getCofactor(mat, temp, 0, f, n);
D += sign * mat[0][f] * determinantOfMatrix(temp, n - 1);
sign = -sign;
}
return D;
}
There is no way to pass a pointer to an array of dynamic size. And the inner dimensions of an array cannot be dynamic anyway.
What you can do instead is use a one dimensional dynamic array, pass pointer to element of that array as usual, and store the rows one after the other, and calculate the index based on the row size passed as an array. This results in the same layout as an array of arrays would have, but the "virtual" dimensions can be dynamic.
I think, I have just made a mistake: I was allocating a static 2D array and accessing it as 1 dimension.
Could you tell me how bad it is - method geta?
The code below works fine on my Windows, and Linux: actual is always eqauls to expected and stride is always equals to N.
#include "stdafx.h"
#define N 2000
int a[N][N];
int geta(int i, int j) {
return *(a[0] + i * N + j);
}
int main()
{
printf("Hello\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i][j] = i + j;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int const expected = a[i][j];
int const actual = geta(i, j);
if (actual != expected) {
printf("wrong data at [%d,%d] expected=%d actual=%d", i, j, expected, actual);
}
}
}
for (int i = 1; i < N; i++) {
int stride = a[N] - a[N - 1];
if (stride != N) {
printf("wrong: i=%d c=%d N=%d", i, stride, N);
}
}
return 0;
}
Could you tell me how bad it is - method geta?
Bad. But correct. C++ guarantees memory for a to be contiguous and the memory layout to be row major, so your code returns in a correct manner the expected element. Let's see how:
The type of a[0] is int[2000] 1) . But as soon as you do arithmetic on it it decays, i.e. int*. So +i*N moves the pointer to the (beginning of the) line i and +j moves the pointer to the column j.
1) actually it's int(&)[2000] but not that relevant here
Does anyone know how to add a vector into an array?
I'm given a array table but to solve collisions, i need to implement vectors.
I am provided with this code:
void generateTable(string DNA, int N, int K, int *&table) {
int tableSize = 1;
for (int i = 0; i < K; i++)
table[i] = 0;
I had planned to implement the hashing as such:
for(int i = 0; i < N - K; i++) {
temp = DNA.substr(i, K);
for (int j = 0; j < K; j++) {
value = value * 10 + convert(temp[j]) //this is just to convert the substr into int.
if (table[value % tableSize] == 0)
table[value % tableSize] = value;
else {
//this is where i plan to use vectors to solve collisions
thank you so much for your help. Do let me know if there is any confusion about my question
I'm trying to implement the bubble sorting algorithm to an array of integers, the function which sorts the array takes an array as a parameter and suppose to return the sorted array.
Here is the code:
#include <iostream>
using namespace std;
int* BubbleSort(int data[]){
for(int i=0; i<sizeof(data)/sizeof(data[0])-1; i++){
for(int j=0; j<sizeof(data)/sizeof(data[0])-1-i; j++){
if(data[j+1]>data[j]){
int temp = data[j+1];
data[j+1]=data[j];
data[j]=temp;
}
}
}
return data;
}
int main()
{
int data[]={8,4,9,7,6,5,13,11,10};
int *a=BubbleSort(data);
cout<<"{";
for(int i=0; i<sizeof(data)/sizeof(data[0]); i++){
cout<<a[i];
if(i==sizeof(data)/sizeof(data[0])-1){
cout<<"}"<<endl;
}else{
cout<<",";
}
}
return 0;
}
The output I'm getting:
{8,4,9,7,6,5,13,11,10}
You must pass in the size of the array because an array it decays to the pointer to its first element (element 0).
void BubbleSort(int data[], int size){
for(int i(0); i != size; ++i){
for(int j(i + 1); j != size; ++j){
if(data[i] > data[j]){
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
Maybe too late, but maybe in the future it will be useful,
Try to use this code
...
/**
* Sort array of integers with Bubble Sort Algorithm
*
* #param arr Array, which we should sort using this function
* #param arrSZ The size of the array
* #param order In which order array should be sort
*
* #return Sorted array of integers
*/
void bubbleSortInt(double arr[], int arrSz, string order = "ascending")
{
for (int i = 0; i < arrSz; ++i)
{
for (int j = 0; j < (arrSz - i - 1); ++j)
{
// Swapping process
if ((order == "descending") ? arr[j] < arr[j + 1] : arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
return; // Optional because it's a void function
}// end bubbleSortInt
...
Then use it in your main function like below,
...
double integers[array_size];
const int array_size = 10;
string order = "ascending";
bubbleSortInt(integers, array_size, order)
for (int i = 0; i < array_size; ++i)
{
cout << integers[i] << "\t";
}
...
Have a detailed look at the bubble sort algorithm -> https://github.com/teamroyalcoder/algorithms#bubble-sort-algorithm
This is a GitHub repo where you will find a bunch of algorithms with full details and source code written in c++ https://github.com/teamroyalcoder/algorithms
I'd like to subtract the highest element in this 5x2 matrix, with it's subsequent element, in the other column.
For ex: the highest element right now is 150, whose location is (4,1). I'd like to subtract 150 with 89, which is it's subsequent element. In the same way if the highest element belonged to the first column, then it should subtract itself from the element in the next column.
Thanks
int big=0,lead=0,m,n;
int a[5][2]={140,82,89,150,110,110,112,106,88,90};
for(int j=0; j<5; j++)
{
for(int i=0 ; i<2; i++)
{
m=j;
n=i;
if(a[j][i] > big)
{
big = a[j][i];
if(big == a[j][i])
{
lead = big-a[j][1];
}
else
{
lead = big-a[1][i];
}
}
}
}
cout<<big<<"\n"<<lead<<"\n"<<m<<","<<n<<endl;
}
I think you declare the array wrong, i build 2D array as
int a[2][5] = {
{140,82,89,150,110},
{110,112,106,88,90}
};
First, you want to find the largest using big variable (i assume). So
int big = 0,n,m;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 5; y++){
if(a[x][y] > big){
big = a[x][y];
n = x;
m = y;
}
}
}
Here you save the biggest and it's coordinate at n and m variable.
After that, you want to subtract it with column - 1 (column + 1 if it is on the first column)
if(m == 0){
big -= a[n][m + 1];
}
else{
big -= a[n][m - 1];
}