This code should make a sum of the numbers in the main diagonal of a matrix.
#include <stdio.h>
#define RIG 2
#define COL 5
void sum(unsigned int a[RIG][COL]);
int main(){
unsigned int a[RIG][COL] = {{1,2,3,4,5},{6,7,8,9,10}};
sum(a);
}
void sum(unsigned int a[RIG][COL]){
unsigned int c = 0;
unsigned int j = 0;
if (RIG<=COL){
int n = RIG;
}
else{
int n = COL;
}
for (size_t i=0;i<=n-1;++i){
c += a[i][j];
j += 1;
}
printf("%d\n", c);
}
Output:
matrix_sum.c:28:21: error: use of undeclared identifier 'n'
for (size_t i=0;i<=n-1;++i){
^
1 error generated.
I don't get why I can't use the n variable inside the for loop. How can I do that?
This
if (RIG<=COL){
int n = RIG;
}
else{
int n = COL;
}
does not scope n the way you might think it does. Scoping rules are better explained here, but in short: n exists only within each branch of the if/else block. As such, when you attempt to access n in your for-loop later on, it no longer exists!
This is trivially remedied by bringing n into scope, e.g.
int n = COL;
if (RIG <= COL) {
n = RIG;
}
for (size_t i = 0; i <= n - 1; ++i) {
c += a[i][j];
j += 1;
}
Related
So based in the following problem from cumulative sum query I created the solution. But is any other way to solve the problem in C with linear complexity O(N)?
Problem description:
William Macfarlane wants to look at an array.
You are given a list of N numbers and Q queries. Each query is
specified by two numbers i and j; the answer to each query is the sum
of every number between the range [i, j] (inclusive).
Note: the query ranges are specified using 0-based indexing.
Input
The first line contains N, the number of integers in our list (N <=
100,000). The next line holds N numbers that are guaranteed to fit
inside an integer. Following the list is a number Q (Q <= 10,000). The
next Q lines each contain two numbers i and j which specify a query
you must answer (0 <= i, j <= N-1). Output
Output
For each query, output the answer to that query on its own line in the
order the queries were made.
Here is the solution:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node {
int first;
int last;
};
int sum_array(int *array, int first, int last) {
int sum = 0;
for (int i = first; i <= last; i++) {
sum += array[i];
}
return sum;
}
int main() {
FILE* input = fopen("share.in","r");
int N = 0;
fscanf(input,"%d",&N);
int *array = (int*)malloc(N * sizeof(int));
for (int i = 0; i < N; i++) {
fscanf(input,"%d",&array[i]);
}
int Q = 0;
fscanf(input,"%d",&Q);
struct node query[Q];
for (int i=0; i < Q; i++) {
fscanf(input,"%d",&query[i].first);
fscanf(input,"%d",&query[i].last);
}
fclose(input);
int sum = 0;
for ( int i = 0; i < Q ; i++) {
int first = query[i].first;
int last = query[i].last;
sum = sum_array(array,first,last);
printf("Number of queries : %d , sum is %d\n",i ,sum);
}
free(array);
return 0;
}
Update:
The answer given is good. But for some reason I couldn't make it work.
So here is the code rewritten and if someone can explain me what I do wrong I will be happy! Keep in mind we want the range to be [first,last]
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node {
int first;
int last;
};
int sum_array(int *array, int first, int last) {
int sum = 0;
for (int i = first; i <= last; i++) {
sum += array[i];
}
return sum;
}
int main() {
FILE* input = fopen("share.in","r");
int N = 0;
fscanf(input,"%d",&N);
int *array = (int*)malloc(N * sizeof(int));
int *integralArray = (int*)malloc(N * sizeof(int));
for (int i = 0; i < N; i++) {
fscanf(input,"%d",&array[i]);
integralArray[i] = array[i] + ((i > 0) ? array[i-1] : 0);
}
int Q = 0;
fscanf(input,"%d",&Q);
struct node query[Q];
for (int i=0; i < Q; i++) {
fscanf(input,"%d",&query[i].first);
fscanf(input,"%d",&query[i].last);
}
fclose(input);
int sum = 0;
for (int i = 0; i < Q ; i++) {
int first = query[i].first;
int last = query[i].last;
sum = integralArray[last] - integralArray[first - 1];
printf("Number of queries : %d , sum is %d\n",i ,sum);
}
free(array);
return 0;
}
You'd form the integral array. Modify to something like:
int *array = (int*)malloc(N * sizeof(int));
int *integralArray = (int*)malloc(N * sizeof(int));
for (int i = 0; i < N; i++) {
fscanf(input,"%d",&array[i]);
integralArray[i] = array[i] + ((i > 0) ? integralArray[i-1] : 0);
}
So the element at integralArray[i] is the sum of all elements in array from 0 to i.
Then, to get the sum from a to b, where a > b, integralArray[b] is the sum from 0 to b and integralArray[a] is the sum from 0 to a so you can just compute integralArray[b] - integralArray[a] to get the total from a to b. Intuitively, integralArray[b] includes the numbers you want but it also includes the numbers up to and including a. You don't want those so you take them off again.
Vary appropriately for inclusion or exclusion of the number at a and the number at b. That as given will include the number at b but not that at a. You could adjust your integralArray to be one earlier (so integralArray[b] is the sum from 0 to b-1) or adjust your indices.
I want to reshape an array of length L to one of MxN, however rather than create new memory by copying the elements over in for loops I'd like to do some pointer casting to allow me to access the array with double subscripting (array[X][Y]) .
I've googled around for ages and couldn't find anything helpful.
Any help would really be appreciated.
EDIT: The array of interest is on the heap not stack.
Pointer casting doesn't seem like a good idea here. But if the number of columns is known before run-time, one option I often encounter is to simply define a macro e.g.
#define arrayName(i,j) arrayName[((i)*N) + (j)]
Or
#define ELEMENT(arrayName, i,j) (arrayName)[((i)*N) + (j)]
if you want it to be more flexible.
unions are your friends:
#include <stdio.h>
#define M 5
#define N 4
typedef union {
int a1[M * N];
int a2[M][N];
} U;
int main()
{
U u;
int i, j;
for (i = 0; i < M * N; ++i)
{
u.a1[i] = i;
}
for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
printf("%8d", u.a2[i][j]);
}
printf("\n");
}
return 0;
}
typedefs are your friends:
#include <stdio.h>
#include <stdlib.h>
#define M 5
#define N 4
typedef int Array1D[M * N];
typedef int Array2D[M][N];
int main()
{
Array1D *a1 = malloc(sizeof(*a1));
Array2D *a2 = (Array2D *)a1;
int i, j;
for (i = 0; i < M * N; ++i)
{
(*a1)[i] = i;
}
for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
printf("%8d", (*a2)[i][j]);
}
printf("\n");
}
free(a1);
return 0;
}
Note: above code has now been updated to use heap allocation so as not to break strict aliasing rules.
By the way if you want it super dynamic (as in no sizes are known at compile-time) you can do this by not copying all the values but by creating an index for the 2nd dimension like so:
#include <stdio.h>
#include <stdlib.h>
const int FULL = 100;
const int X = 10;
const int Y = 10;
int *dim1;
int **nested;
int main(void) {
dim1 = malloc(sizeof(int)*FULL);
nested = malloc(sizeof(int*)*X);
for(int i=0; i<X;i++)
{
nested[i] = &dim1[Y*i];
}
dim1[15] = 42;
printf("nested[1][5]: %d",nested[1][5]);
free(nested);
free(dim1);
return 0;
}
So.. I have something like this. It is supposed to create arrays with 10, 20, 50 100 .. up to 5000 random numbers that then sorts with Insertion Sort and prints out how many comparisions and swaps were done .. However, I am getting a runtime exception when I reach 200 numbers large array .. "Access violation writing location 0x00B60000." .. Sometimes I don't even reach 200 and stop right after 10 numbers. I have literally no idea.
long *arrayIn;
int *swap_count = (int*)malloc(sizeof(int)), *compare_count = (int*)malloc(sizeof(int));
compare_count = 0;
swap_count = 0;
int i, j;
for (j = 10; j <= 1000; j*=10) {
for (i = 1; i <= 5; i++){
if (i == 1 || i == 2 || i == 5) {
int n = i * j;
arrayIn = malloc(sizeof(long)*n);
fill_array(&arrayIn, n);
InsertionSort(&arrayIn, n, &swap_count, &compare_count);
print_array(&arrayIn, n, &swap_count, &compare_count);
compare_count = 0;
swap_count = 0;
free(arrayIn);
}
}
}
EDIT: ok with this free(arrayIn); I get this " Stack cookie instrumentation code detected a stack-based buffer overrun." and I get nowhere. However without it it's "just" "Access violation writing location 0x00780000." but i get up to 200numbers eventually
void fill_array(int *arr, int n) {
int i;
for (i = 0; i < n; i++) {
arr[i] = (RAND_MAX + 1)*rand() + rand();
}
}
void InsertionSort(int *arr, int n, int *swap_count, int *compare_count) {
int i, j, t;
for (j = 0; j < n; j++) {
(*compare_count)++;
t = arr[j];
i = j - 1;
*swap_count = *swap_count + 2;
while (i >= 0 && arr[i]>t) { //tady chybí compare_count inkrementace
*compare_count = *compare_count + 2;
arr[i + 1] = arr[i];
(*swap_count)++;
i--;
(*swap_count)++;
}
arr[i + 1] = t;
(*swap_count)++;
}
}
I am sure your compiler told you what was wrong.
You are passing a long** to a function that expects a int* at the line
fill_array(&arrayIn, n);
function prototype is
void fill_array(int *arr, int n)
Same problem with the other function. From there, anything can happen.
Always, ALWAYS heed the warnings your compiler gives you.
MAJOR EDIT
First - yes, the name of an array is already a pointer.
Second - declare a function prototype at the start of your code; then the compiler will throw you helpful messages which will help you catch these
Third - if you want to pass the address of a simple variable to a function, there is no need for a malloc; just use the address of the variable.
Fourth - the rand() function returns an integer between 0 and RAND_MAX. The code
a[i] = (RAND_MAX + 1) * rand() + rand();
is a roundabout way of getting
a[i] = rand();
since (RAND_MAX + 1) will overflow and give you zero... If you actually wanted to be able to get a "really big" random number, you would have to do the following:
1) make sure a is a long * (with the correct prototypes etc)
2) convert the numbers before adding / multiplying:
a[i] = (RAND_MAX + 1L) * rand() + rand();
might do it - or maybe you need to do some more casting to (long); I can never remember my order of precedence so I usually would do
a[i] = ((long)(RAND_MAX) + 1L) * (long)rand() + (long)rand();
to be 100% sure.
Putting these and other lessons together, here is an edited version of your code that compiles and runs (I did have to "invent" a print_array) - I have written comments where the code needed changing to work. The last point above (making long random numbers) was not taken into account in this code yet.
#include <stdio.h>
#include <stdlib.h>
// include prototypes - it helps the compiler flag errors:
void fill_array(int *arr, int n);
void InsertionSort(int *arr, int n, int *swap_count, int *compare_count);
void print_array(int *arr, int n, int *swap_count, int *compare_count);
int main(void) {
// change data type to match function
int *arrayIn;
// instead of mallocing, use a fixed location:
int swap_count, compare_count;
// often a good idea to give your pointers a _p name:
int *swap_count_p = &swap_count;
int *compare_count_p = &compare_count;
// the pointer must not be set to zero: it's the CONTENTs that you set to zero
*compare_count_p = 0;
*swap_count_p = 0;
int i, j;
for (j = 10; j <= 1000; j*=10) {
for (i = 1; i <= 5; i++){
if (i == 1 || i == 2 || i == 5) {
int n = i * j;
arrayIn = malloc(sizeof(long)*n);
fill_array(arrayIn, n);
InsertionSort(arrayIn, n, swap_count_p, compare_count_p);
print_array(arrayIn, n, swap_count_p, compare_count_p);
swap_count = 0;
compare_count = 0;
free(arrayIn);
}
}
}
return 0;
}
void fill_array(int *arr, int n) {
int i;
for (i = 0; i < n; i++) {
// arr[i] = (RAND_MAX + 1)*rand() + rand(); // causes integer overflow
arr[i] = rand();
}
}
void InsertionSort(int *arr, int n, int *swap_count, int *compare_count) {
int i, j, t;
for (j = 0; j < n; j++) {
(*compare_count)++;
t = arr[j];
i = j - 1;
*swap_count = *swap_count + 2;
while (i >= 0 && arr[i]>t) { //tady chybí compare_count inkrementace
*compare_count = *compare_count + 2;
arr[i + 1] = arr[i];
(*swap_count)++;
i--;
(*swap_count)++;
}
arr[i + 1] = t;
(*swap_count)++;
}
}
void print_array(int *a, int n, int* sw, int *cc) {
int ii;
for(ii = 0; ii < n; ii++) {
if(ii%20 == 0) printf("\n");
printf("%d ", a[ii]);
}
printf("\n\nThis took %d swaps and %d comparisons\n\n", *sw, *cc);
}
You are assigning the literal value 0 to some pointers. You are also mixing "pointers" with "address-of-pointers"; &swap_count gives the address of the pointer, not the address of its value.
First off, no need to malloc here:
int *swap_count = (int*)malloc(sizeof(int)) ..
Just make an integer:
int swap_coint;
Then you don't need to do
swap_coint = 0;
to this pointer (which causes your errors). Doing so on a regular int variable is, of course, just fine.
(With the above fixed, &swap_count ought to work, so don't change that as well.)
As I told in the comments, you are passing the addresses of pointers, which point to an actual value.
With the ampersand prefix (&) you are passing the address of something.
You only use this when you pass a primitive type.
E.g. filling the array by passing an int. But you are passing pointers, so no need to use ampersand.
What's actually happening is that you are looking in the address space of the pointer, not the actual value the pointer points to in the end. This causes various memory conflicts.
Remove all & where you are inputting pointers these lines:
fill_array(&arrayIn, n);
InsertionSort(&arrayIn, n, &swap_count, &compare_count);
print_array(&arrayIn, n, &swap_count, &compare_count);
So it becomes:
fill_array(arrayIn, n);
InsertionSort(arrayIn, n, swap_count, compare_count);
print_array(arrayIn, n, swap_count, compare_count);
I also note that you alloc memory for primitive types, which could be done way simpler:
int compare_count = 0;
int swap_count = 0;
But if you choose to use the last block of code, DO use &swap_count and &compare_count since you are passing primitive types, not pointers!
My app is supposed to take a double array, find the average of elements of even columns, find the max value, compare average to max / 2 and rotate the matrix 90 degrees if average > max / 2.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int M = 4, N = 4;
int ** rotateArr(int arr[4][4]) {
int D[4][4];
int i, n;
for(i=0; i <= 4; i++ ){
for(n =0; n <= 4; n++){
D[i][n] = arr[n][M - i + 1];
}
}
return D;
}
int getAvg(int arr[4][4]) {
int sum = 0, num = 0;
int i, n;
for(i=0; i <= 4; i += 2){
for(n=0; n <= 4; n++){
sum += arr[i][n];
num += 1;
}
}
return sum/num;
}
int ** getMax(int arr[4][4]) {
int maxa = arr[0][0];
int i, n;
for(i=0; i <= 4; i++){
for(n=0; n <= 4; n++){
if (maxa < arr[i][n]){
maxa = arr[i][n];
}
}
}
return maxa;
}
int main()
{
int S[4][4] = { { 1, 4, 10, 3 }, { 0, 6, 3, 8 }, { 7, 10 ,8, 5 }, { 9, 5, 11, 2} };
int maxa = 0;
float avg = 0;
avg = getAvg(S);
maxa = getMax(S);
int i , n;
if (avg > maxa/2){
S[4][4] = rotateArr(S);
for(i=0; i <= 4; i+=2){
for(n=0 ; n <= 4; n++){
printf("%d", S[i][n]);
}
printf("\n");
}
}
getch();
return 0;
}
The app doesn't output anything and just ends on key press with
Process terminated with status 0 (0 minutes, 2 seconds)
There are a lot of problems with your code. For example:
All your loops are of the form for(i=0; i <= 4; i++ ) The condition should be changed to i < 4 because the valid array indices are 0 to 3.
The rotateArr function returns a pointer to a local variable. You can't do this. One solution is to receive the output array as a parameter and write into it:
void rotateArr(int arr[4][4], int output[4][4]) {
....
output[i][j] = ...;
}
int** is not the same as int[4][4].
S[4][4] = ... tries to assign to an invalid element. It looks like you're trying to assign to S itself, which can't be done (you need to assign each element, or memcpy from another array):
int anArray[4][4];
int anotherArray[4][4];
memcpy(anArray, anotherArray, sizeof(anArray));
In the expression arr[n][M - i + 1], the second index can be out of range. Consider what happens when i==0.
I suggest you pay attention to compiler warnings, as they would catch some of these issues (on GCC compiler use the -Wall option). Also, learn to use a debugger.
Analyse your program and you will find that;
You are returning D (of type int (*)[4] from rotateArr whose return type is int **. And similar issue with getMax.
For n =4 array arr[i][n] will go out of bound!
sum/num is in fact not calculating the average value (numerator and denominator both are int and you will always get an int value (may be 0 too).
and many more..........
First problem: 0 errors, 0 warnings. Sure the code is correct. Tell me what is wrong? (This is part of program.) I can't understand what is wrong. At least it will display array[3][3] = {{1,1,1},{1,1,1},{1,1,1}}
Second problem: But instead of 'zero's I see clear fields. (I see nothing) But if there is {{1,1,1},{1,1,1},{1,1,1}}, I see '1's... Tell me why?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define L 3 /* Including the [0]-element */
//#define C 3 /* Including the [0]-element */
#define RANGE 100 /* Set up the range of random values */
int fill_in(int *, int, int); /* Prototype of function */
int main()
{
int L = 3, C = 3;
int array[L][C]; // L - Line, C - Column
int i, j; // Global variables
int * aPtr;
aPtr = &array[L][C];
srand((unsigned)time(NULL));
fill_in(aPtr, L - 1, C - 1);
/* Displaying array (AFTER) */ // <--- going to make a function, but my first one
printf("\nAFTER:\n"); // doesn't work correctly =(
for (i = 0; i <= L - 1; i++)
{
for (j = 0; j <= C - 1; j++)
printf("%2.d ", array[i][j]);
printf("\n");
}
return 0;
}
int fill_in( int * aptr, int m, int n) /// PROBLEM? O_о
{
int arr[m][n];
int i, j; // Local variables
/* Filling array with random values */
for (i = 0; i <= m - 1; i++)
{
for (j = 0; j <= n - 1; j++)
arr[i][j] = 1; //1 + rand()%RANGE; // If each element == 1, it works!
}
return arr[i][j];
}
UPDATE:
I've solved it!
Explanations is in comments of the code.
The following code works properly:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Lines 2 /* Including the [0]-element */
#define Columns 2 /* Including the [0]-element */
#define RANGE 100 /* Set up the range of random values */
int fill_in(int*, int, int); /* Prototypes of functions */
int display(int*, int, int);
int main()
{
int L = Lines, C = Columns;
int array[L][C]; // L - Line, C - Column
int* aPtr;
aPtr = &array[0][0];
srand((unsigned)time(NULL));
fill_in(aPtr, L, C); /* Filling array with random values. */
display(aPtr, L, C); /* Displaying array. */
return 0;
}
////////////////////////////////////////////////////////////////////
/** Eureka! The fact is that pointer of a[0][0] is *ptr[0], and
the pointer of a[2][2] is *ptr[8] ---> The 8-th element of the array[2][2].
>>>> Pointer sees array[][] not as matrix (square), but as a line!
>>>> As if to make a line of a square matrix!
*/
int fill_in(int* aptr, int m, int n) /* Filling array with random values. */
{
int i; // Local variables
int max_number_of_element = ((m+1)*(n+1)-1);
for (i = 0; i <= max_number_of_element; i++)
*(aptr + i) = 1 + rand()%RANGE;
return *aptr;
}
int display(int* aptr, int m, int n) /* Displaying array. */
{
int i;
int count = 1;
int max_number_of_element = ((m+1)*(n+1)-1);
for (i = 0; i <= max_number_of_element; i++)
{
printf("%2.d ", *(aptr + i));
if (count % (n+1) == 0)
printf("\n");
count++;
}
return i;
}
Instead of:
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
aptr[i * n + j] = 1;
I've used:
for (i = 0; i <= max_number_of_element; i++)
*(aptr + i) = 1;
//Pointer sees array[][] not as matrix (square), but as a direct sequence (line).
Not sure whether it does anything outside of the array, but the main problem is solved.
P.S.
Let me know if I was wrong.
You don't do anything with aptr in the fill_in function. Instead, you fill the local arr array. Do this instead:
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
aptr[i * n + j] = 1;
}
Furthermore, this:
aPtr = &array[L][C];
needs to ne
aPtr = &array[0][0];
You want a pointer to the first element of your array.
Also, instead of counter <= max - 1, use the more concise and conventional counter < max notation.
This is wrong
int * aPtr;
aPtr = &array[L][C];
You are assigning a pointer to a position out of array bounds. The array is defined from 0-2 on both lines and columns, and you are assigning position (3,3) to the pointer.
Besides this, you are doing nothing with the pointer you pass as argument to the fill_in function.