Pointers exit code 6 - c

Well i m struggling with the pointers, why doesn t work the function biggest in the end (yes of that s dummy function)? (exit code 6)
Ty for help
code:
int search(int const a[], int n, int key) {
for (int *i = a; i < a + n; i++) {
if ( key == *i ) return 1;
}
return 0;
}
void print_row(int const a[], int n, int row) {
for (int *i = a + n * row; i < a + n * (row + 1); i++) {
printf("%d ", *i);
}
printf("\n");
}
void biggest(double x, long *int_part, double *frac_part) {
*int_part = (long) x;
*frac_part = x - *int_part;
}
main () {
int tempretures[7][24];
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
tempretures[6][5] = 32;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 24; j++) {
printf("%d ", tempretures[i][j]);
}
printf("\n");
}
printf("Is it: %d\n", search(tempretures, 7*24, 32));
for (int i = 0; i < 7; i++) {
print_row(tempretures, 24, i);
}
long a = 0; double b = 0;
biggest(5.67, &a, &b);
printf("%li", a);
}

for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
This runs way past the end of the array, stopping only when it hits every element of row number 7, but there is no row number 7.

Related

lottery generation function using 'void'

i need to use 'void lotto_gen(void)' function but i don't know how to make this => lotto[i + (j * 5)] = lotto_gen(); right. output should look like this picture enter image description here
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void lotto_gen()
{
int ball = 1 + rand() % 45;
}
void swap(int* a, int* b)
{
int r;
r = *a;
*a = *b;
*b = r;
}
void shuffle(int* arr, int size)
{
int i, r;
for (i = 0; i < size; i++)
{
r = rand() % (size - i) + i;
swap(arr + i, arr + r);
}
}
int main(void)
{
int i, j;
int lotto[50] = { 0 };
int size = sizeof(lotto) / sizeof(lotto[0]);
srand(time(0));
for (j = 0; j < 10; j++)
{
for (i = 0; i < 5; i++)
lotto[i + (j * 5)] = lotto_gen();
}
for (j = 0; j < 10; j++)
{
printf("[");
for (i = 0; i < 5; i++)
printf("%d ", lotto[i + (j * 5)]);
printf("]\n");
}
return 0;
}
From your comment, "It states that the problem does not require parameters in lotto_gen()" -- If this is only the problem constraint, then you have your freedom to choose the return type. Why not go with int lotto_gen(void) instead of void lotto_gen(void). You can have your lotto_gen() function like this:
/* ..... */
int lotto_gen(void)
{
return (1 + rand() % 45);
}
/* .... */

My function doesn't do anything with arrays

Function could replace first n elements from array A with last n elements of array B.
Output is
Array A
1
2
3
4
5
Array B
6
7
8
9
10
I tried to put printf (i) in every loop and it seems to be work fine but it do nothing with arrays a and b :(
void reparray(int *a, int *b, int n){
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i<n; i++){
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element>=n; last_element--){
help[last_element] = b[last_element];
}
i = 0;
for (i; i<n; i++){
a[i] = help[i];
}
last_element = 4;
for (last_element;last_element >= n; last_element--){
b[last_element] = help[last_element];
}
}
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int b[5] = {6, 7, 8, 9, 10};
int n,i;
reparray(a, b, 2);
printf("Array A\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", a[i]);
}
printf("Array B\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", b[i]);
}
return 0;
}
your 2 last loops in void reparray do nothing except copying exact things which were already in a and b
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4-1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
i = 0;
for (i; i< n; i++) {
b[i] = help[i];
}
}
for other input change void reparray
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4 - 1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
last_element = 4;
int first_element = 0;
for (i = last_element - n + 1; i < last_element + 1; i++) {
b[i] = help[first_element];
first_element++;
}
}

What's wrong with the function multiplyTwoMatrices() in this C code for finding fibonacci using matrix multiplication?

#include<stdio.h>
void multiplyTwoMatrices(int (*)[2], int[][2], int[][2]);
void copyMatrix(int[][2], int[][2]);
void powerAMatrix(int[][2], int[][2], int);
int main()
{
int num;
scanf("%d", &num);
int i;
for(i = -1; i <= num; i++)
{
printf("\n%d", fib(i));
}
}
int fib(int num)
{
if(num <= 0)
{
return 0;
}
else
{
int matrix[2][2] = {{1, 1}, {1, 0}};
//int fibMatrix[2][2] = powerAMatrix(matrix[2][2], num);
int fibMatrix[2][2];
powerAMatrix(fibMatrix, matrix, num);
return getFibNum(fibMatrix);
}
}
void powerAMatrix(int fibMatrix[2][2], int matrix[2][2], int num)
{
//fibMatrix = matrix;
copyMatrix(fibMatrix, matrix);
int i = 0;
for(i = 1; i < num; i++)
{
//fibMatrix = fibMatrix * matrix;
multiplyTwoMatrices(fibMatrix, fibMatrix, matrix);
}
}
void copyMatrix(int destinationMatrix[2][2], int sourceMatrix[2][2])
{
int i = 0; int j = 0;
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
destinationMatrix[i][j] = sourceMatrix[i][j];
}
void multiplyTwoMatrices(int multipliedMatrix[2][2], int matrixA[2][2], int matrixB[2][2])
{
int i = 0; int j = 0; int k = 0;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
multipliedMatrix[i][j] = 0; //or just initialize it as a zero matrix.
for(k = 0; k < 2; k++)
{
multipliedMatrix[i][j] += (matrixA[i][k] * matrixB[k][j]);
}
}
}
//working alternative
/*
int x = matrixA[0][0]*matrixB[0][0] + matrixA[0][1]*matrixB[1][0];
int y = matrixA[0][0]*matrixB[0][1] + matrixA[0][1]*matrixB[1][1];
int z = matrixA[1][0]*matrixB[0][0] + matrixA[1][1]*matrixB[1][0];
int w = matrixA[1][0]*matrixB[0][1] + matrixA[1][1]*matrixB[1][1];
multipliedMatrix[0][0] = x;
multipliedMatrix[0][1] = y;
multipliedMatrix[1][0] = z;
multipliedMatrix[1][1] = w;
*/
}
int getFibNum(int fibMatrix[2][2])
{
return fibMatrix[0][1];
}
The function multiplyTwoMatrices() seems to work only if "working alternative" (code closed in comments inside this function) is used instead of the one used currently. I'm unable to understand what is going wrong with this code. A little help is appreciated.
Output expected: 0 0 1 1 2 3 5 8 ...
Output coming: 0 0 1 1 1 1 1 1 ...
while your multiplication function is correct, it doesn't work correctly when the destination is one of the operands; if it is, the operand is changed there while the calculation is underway.
Either require that multipliedMatrix is distinct from both matrixA and matrixB (that'd be preferred), or have a temporary matrix there and copy it into the result!
P.S. it would be much easier wrapping the matrix class into a struct:
struct intmatrix2x2 {
int values[2][2];
};
this would make implicit copies when calling functions; and instead of copyMatrix you can say:
struct intmatrix2x2 b = a;
and your multiplication could read as
struct intmatrix2x2 multiply(struct intmatrix2x2 a, struct intmatrix2x2 b)
{
struct intmatrix2x2 result;
int i = 0; int j = 0; int k = 0;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
result.values[i][j] = 0; //or just initialize it as a zero matrix.
for(k = 0; k < 2; k++)
{
result.values[i][j] += a.values[i][k] * b.values[k][j]);
}
}
}
return result;
}
and you could use it as
struct intmatrix2x2 result = multiply(a, b);

print 2D matrix in C

I'm trying to create and print a matrix, but I'm getting segmentation fault.
int** init_dynamic_matrix (int l, int c);
void print_dynamic_matrix (int** ppints, int l, int c);
int main ()
{
int** ppints = NULL;
int l = 6, c = 3;
ppints = init_dynamic_matrix (l, c);
print_dynamic_matrix (ppints, l, c);
return 0;
}
int** init_dynamic_matrix (int l, int c)
{
int i = 0, j = 0;
int** ppaux = NULL;
ppaux = (int**) malloc (l * (sizeof (int*)));
for (i = 0; i < l; i++)
{
*(ppaux + i) = (int*) malloc (c * (sizeof (int)));
for (j = 0; j < c; j++)
{
ppaux[l][c] = 0;
}
}
return ppaux;
}
void print_dynamic_matrix (int** ppints, int l, int c)
{
int i = 0, j = 0;
for (i = 0; i < l; i++)
{
for (j = 0; j < c; j++)
{
printf ("%d", ppints[l][c]);
}
printf("\n");
}
}
for (j = 0; j < c; j++)
{
ppaux[l][c] = 0;
}
Here's the error: in the first iteration of the outer for loop you will execute this , but ppaux[l] is not yet initialized, so you get segmentation fault.
You probably meant something like:
for (j = 0; j < c; j++)
{
ppaux[i][j] = 0;
}
Because you just created the i-th row of your matrix and you want to se it to 0

Finding the smallest element in multidimensional array

Im asking for help once again. This time I found myself in the midst of a task which I found on the internet and I cant find a good solution. At first I thought of bubble sort, but I didnt seem to find a way to use it with multidimensional array.
The task asks to find 3 smallest elements which arent repeating on the second array.
So far my code looks like this, though Ive removed a lot of it... Since it was a very wrong and futile aproach.
Everything was written in C.
Thanks for the help.
3
1 2 3
3 3 3
3 3 3
5 4 6
6 4 5
4 5 6
This is the given information for the file
#include stdio.h
#include stdlib.h
int main()
{
FILE *fptr;
fptr = fopen("Duomenys.txt", "r");
int i, j;
int k, l;
int bubbleTmp;
fscanf(fptr, "%d ", &k);
l = k;
int a[k][k];
int b[k][k];
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
fscanf(fptr,"%d ",&a[i][j]);
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n\n\n");
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
fscanf(fptr,"%d ",&b[i][j]);
printf("%d ", b[i][j]);
}
printf("\n");
}
// Bubble Sort
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
if(a[i][j] > a[i][j+1])
{
}
}
}
fclose(fptr);
return 0;
}
To find the minimum and its position in the array:
int min = a[0][0], i_min = 0, j_min = 0;
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
if(a[i][j] < min)
{
min = a[i][j];
i_min = i;
j_min = j;
}
}
}
After the first try, you repeat it two more times, avoiding the minimums alrerady found. The positions of each minimums are known.
Previously, it was a suggestion.
Now, here is the solution:
//----------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <ctime>
//----------------------------------------------------------------------------
int **CreateArray (int N, int M)
{
int **arr = new int*[N];
for (int i = 0; i<N; i++)
arr[i] = new int [M];
for (int i = 0; i<N; i++)
for (int j = 0; j<M; j++)
arr[i][j] = rand()%10 - rand()%10;
return arr;
}
void DestroyArray (int **arr, int N)
{
for (int i = 0; i<N; i++)
delete [] arr[i];
delete [] arr;
}
void PrintArray (int **arr, int N, int M)
{
for (int i = 0; i<N; i++)
{
for (int j = 0; j<M; j++)
std::cout << std::setw(7) << arr[i][j];
std::cout << "\n";
}
std::cout << "\n";
}
//----------------------------------------------------------------------------
typedef struct _min
{
int value;
int i;
int j;
} TMin;
//---------------------------------------------------------------------------
void FindMin (int **arr, int N, int M, TMin **mins, int min_count)
{
mins[min_count]->value = arr[0][0];
mins[min_count]->i = 0;
mins[min_count]->j = 0;
for (int i = 0; i<N; i++)
for (int j = 0; j<M; j++)
{
if (arr[i][j] < mins[min_count]->value)
{
bool prev = false;
for (int k = 0; k<min_count; k++)
if (i == mins[k]->i && j == mins[k]->j)
{
prev = true;
break;
}
if (!prev)
{
mins[min_count]->value = arr[i][j];
mins[min_count]->i = i;
mins[min_count]->j = j;
}
}
}
}
//----------------------------------------------------------------------------
int main()
{
//array creation, filling, printing
srand ((unsigned int)time (NULL));
int N = 6, M = 7;
int **arr = CreateArray (N, M);
PrintArray (arr, N, M);
//container for 3 minimums
TMin **mins = new TMin*[3];
for (int i = 0; i<3; i++)
mins[i] = new TMin;
//let us find them
int min_count = -1;
while (++min_count < 3)
FindMin (arr, N, M, mins, min_count);
//result
for (int i = 0; i<3; i++)
std::cout << "arr[" << mins[i]->i << "][" <<mins[i]->j << "] = " << mins[i]->value << "\n";
//cleaning memory
for (int i = 0; i<3; i++)
delete [] mins[i];
delete [] mins;
DestroyArray (arr, N);
std::cin.sync();
std::cin.get();
return 0;
}
//-----------------------------------------------------------------------------

Resources