Ok, so I'm writing a program to make a 10 x 10 array filled with random numbers between 0 & 9, and (with each step organized into a function):
(a) sum the first row and print it out
(b)print out average of main diagonal (top to bottom, left to right)
(c)print out how many 0's are in the first column
(d)make more 10 x 10 arrays with random numbers between 0 & 9 and if all the values in the main
diagonal (top to bottom, left to right) are 7 or greater, print out the array, and the
amount of tries it took. If it can't do it in 1,000,000 attempts, print that it could not
be done.
(e)make a 1D dynamically allocated array containing 10 numbers between -10 & +10, multiply by
the first array made and display the resulting vector
Can't figure out what's not making it work, getting the wrong values for all the steps when they're printed out :'( and some errors
void simple_array(int ten_by_ten[10][10])
{
int i, j;
printf("\n");
for (i=0; i<10; ++i)
{
for (j=0; j<10; ++j)
{
ten_by_ten[i][j] = rand() % 10;
printf("%d ", ten_by_ten[i][j]);
}
printf("\n");
}
}
void sum_first_row(int y[10][10])
{
int i = 0, j, sum_row = 0;
for (j=0; j<10; ++j)
{
sum_row += y[i][j];
}
printf("\nThe sum of the first row is: %d\n", sum_row);
}
void average_main_diagonal(int z[10][10])
{
int i, j = 0, average_diagonal = 0;
for (i=0; i<10; ++i)
{
++j;
average_diagonal += z[i][j];
}
printf("\nThe average of the diagonal is: %lf\n", (average_diagonal / 10.0));
}
void zeros(int a[10][10])
{
int i, j = 0, zeroz = 0;
for (i=0; i<10; ++i)
{
if (a[i][j] == 0)
++zeroz;
}
printf("\nThere are %d zero's in the first column\n", zeroz);
}
void multiple_arrays()
{
int sum_diagonal = 0,array[10][10], i, j, k, l, c;
while ((sum_diagonal < 70) && (c <= 1000000))
{
j = 0;
k = 0;
l = 0;
i = 0;
for (i=0; i<10; ++i)
{
for (j=0; j<10; ++j)
{
array[i][j] = rand() % 10;
}
}
for (k=0; k<10; ++k)
{
++l;
sum_diagonal += array[k][l];
}
++c;
}
if (c = 1000000)
printf("\nCould not get a diagonal with numbers >= 7\n");
else
{
j = 0;
i = 0;
for (i=0; i<10; ++i);
{
printf("\n");
for (j=0; j<10; ++j)
printf("%d ", array[i][j]);
}
printf("It took %d many tries to get a diagonal with all numbers >= 7", c);
}
}
void array_multiplication(int b)
{
int **arrays, i, j, k, l, m, prod[10];
arrays = (int **) calloc (10, sizeof(int *));
for (i=0; i<10; ++i)
arrays[i] = (int *) calloc (1, sizeof(int));
for (i=0; i<10; i=i+1)
{
arrays[i] = (rand() % 21) -10;
}
for (k=0; k<10; ++k)
{
prod[k] = 0;
for (l=0; l<10; ++l)
prod[k] = prod[k] + b[k][l] * arrays[l];
}
printf ("The product is: <");
for (m=0; m<10; ++m)
printf ("%d, ", prod[m]);
printf (">\n");
}
int main()
{
int x[10][10];
simple_array(x);
sum_first_row(x)
average_main_diagonal(x);
zeros(x);
multiple_arrays();
array_multiplication(x);
return (0);
}
Getting the following errors:
When I comment out the "array multiplication" function (because it's getting the following errors: (a) assignment makes pointer from integer without cast "arrays[i] = (rand() % 21) -10;" (b) value is neither array nor pointer "prod[k] = prod[k] + b[k][l] * arrays[l];" (c) passing arg1 of "array_multiplication" makes integer from pointer without cast "array_multiplication(x);"
and it prints out an incorrect average of the diagonal
Help is Extremely appreciated!!!
Thanks,
--Rob
arrays[i] = (rand() % 21) -10; : arrays[i] is a pointer. You can't assign an integer to a pointer. Actually you just assigned some allocated memory to arrays[i] on the previous line, so even if this worked you would leak that memory.
Maybe you meant to have two nested loops, if you want to put a value in every row and every column?
Also you never free the memory you calloc'd. And don't cast the value returned by calloc either.
The other errors, you meant to declare your function as
void array_multiplication(int b[10][10])
instead of (int b). It is complaining that you are doing array operations on an int.
Your code that looks at the diagonals is incorrect (which you knew). You are incrementing "j" before using it...and so "i" is 0 and "j" is 1...which is not the diagonal. You either need to do your "++j" AFTER using it to look up the value....but would be better suited to just use z[i][i] (use "i" for BOTH indices).
This problem happens in both average_main_diagonal and multiple_arrays.
Related
My general goal with this question is to understand how to make use (say, print) of an array returned from an outer function. I'm using prime numbers as an example.
I'm new so going in-depth explaining... Sorry if there's un-needed info or misused terms :)
I defined a function "findprimes" to find every prime between 1 and x.
The function successfully prints an array of primes. However, I don't know if it returns the array of primes. And, if it does, I have no idea how to use said array in main().
This is the function 'findprimes':
int* findprimes(int x) {
int i, j, total=0;
int* numbers = (int*)malloc((x + 1) * sizeof(int));
/* initialization */
for (i = 0; i <= x + 1; i++) {
numbers[i] = 1;
}
/* find primes and assign prime=1 */
for (i = 2; i < sqrt(x); i++) {
if (numbers[i] == 1) {
for (j = i * i; j <= x; j = j + 2) {
if (j % i == 0) {
numbers[j] = 0;
}
}
}
}
/* count how many primes */
for (i = 1; i <= x; i++) {
if (numbers[i] == 1) {
total++;
}
}
/* put primes into their own array */
int* primes = (int*)malloc(total * sizeof(int));
for (i = 1,j=0; i <= x; i++) {
if (numbers[i] == 1) {
primes[j] = i;
j++;
}
}
//I want this part to be in main()
printf("The prime numbers between 1 and %d are: ", x);
for (i = 0; i < total; i++) {
printf("%d ", primes[i]);
}
return primes;
}
I want to print the 'primes' array in main, not in the function 'findprimes' itself. How can I do this?
int main() {
int n;
do {
printf("Enter a value for X>2: ");
scanf("%d", &n);
} while (n <= 2);
findprimes(n); //This returns 'primes' array
//I want to print 'primes' array here
}
Capture the return value in main():
int *primes = findprimes(n);
and use the array:
for (int i = 0; i < some_magic_number; i++)
{
printf("%d\n", primes[i]);
}
and remember to free the memory:
free(primes);
In fact, you also need to free numbers in the function.
The some_magic_number needs to be known — how does the main() function know how many elements are in the array? One way to fix that is to pass a pointer to the function that can hold the number of primes:
int *findprimes(int n, int *num_primes)
and use *num_primes = total; in the function before returning.
You could call the function with:
int some_magic_number;
int *primes = findprimes(n, &some_magic_number);
You'd probably choose an alternative name for the variable in main(), but it illustrates my point.
I am writing this code to print the following matrix in this spiral order(spiral by column).But my code is printing totally different thing.
a a+7 a+8 a+15
a+1 a+6 a+9 a+14
a+2 a+5 a+10 a+13
a+3 a+4 a+11 a+12
Here is what i did:
int main() {
int a;
int Sum = 0;
int i = 0, j = 0,n;
printf("Insert the value of n: ");
scanf("%d",&n);
printf("Insert the value of a number: ");
scanf("%d",&a);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",a);
a = a + 7;
printf("\t");
}
printf("%d",a);
a = a + 1 ;
printf("\n");
}
return 0;
}
The way I approached this is to build the matrix of values you actually want, but doing so in column order, where we can relatively easily control the logic of value progression by row. Then, with that matrix in hand, print out the values in row order, as you want the output:
int main()
{
int a = 7;
int n = 4;
int array[4][4];
for (int c=0; c < n; ++c)
{
for (int r=0; r < n; ++r)
{
// values ascending for even columns
if (c % 2 == 0)
{
array[r][c] = a + c*n + r;
}
// values descending for odd columns
else
{
array[r][c] = a + c*n + n-r-1;
}
}
}
for (int i=0; i < n; ++i)
{
for (int j=0; j < n; ++j)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
Output:
Demo here:
Rextester
Instead of using this complex mechanism to keep track of all elements you can just calculate the value to add at any time by simple arithmetic.
See this
int row;
int column;
printf("\n");
for (row = 0; row < n; row++) {
for (column = 0; column < n; column++) {
int base;
int flag;
if (column % 2 != 0) {
base = (column+1)/2 * 2*n - 1;
flag = -1;
}else {
base = column/2 * 2*n;
flag = 1;
}
printf( "%d ", a + base + flag * row);
}
printf("\n");
}
I hope you are able to follow this logic. If not feel free to ask.
Demo here:
Ideone
There seem to be two issues with your code as it is. As mentioned in the above comment, you are using the variable a in the loop calculation, so it is constantly being updated. This means your loop becomes invalid after a few iterations. If you define a dummy variable, this would avoid the problem. Secondly the implementation of the spiralling is close to being right, but it's not quite there.
Consider in the case n = 4. When you print along each row, the difference between a new element and the last alternates between values of (2n - 1) = 7 and 1. To take this into account, you could for example check every time you want to print whether the column index (j) is odd or even, and use this to determine which difference to add. Once you have the row machinery fixed, it shouldn't be difficult to extend it to the columns.
Simple solution using a matrix to calculate values before print them
#include <stdio.h>
int main(void)
{
int a;
int i = 0, j = 0, n;
printf("Insert the value of n: ");
scanf("%d", &n);
printf("Insert the value of a number: ");
scanf("%d", &a);
int matrix[n][n];
for (i=0; i< n*n; i++)
{
// even columns ascending
if (((i/n) % 2) == 0)
{
matrix[i%n][i/n] = a++;
}
// odd column descending
else
{
matrix[n-(i%n)-1][i/n] = a++;
}
}
for (i=0; i< n; i++)
{
for (j=0; j< n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Insert the value of n: 4
Insert start value: 1
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
I am trying to assign user input into an array; however, the program below only picks up on the first element in each line of input. The ultimate goal of this program is to find the diagonal sums of integers and return the absolute value of their difference.
Example input (note that the first number gives the number of rows and columns (square array):
Input:
3
11 2 4
4 5 6
10 8 -12
Output:
Expected = 15
Actual = 10
I realize that the issue lies in the way that the array is setup. If I print the array out I get: 111555999
Any hints/help would be very appreciated.
int main() {
int n, i, c, multi_array[200][200], sum1 = 0, sum2 = 0;
scanf("%i", &n); //N = number of rows and number of columns (square 2D array)
for (i = 0; i < n; i++) {
for (c = 0; c < n; c++) {
scanf("%d ", &multi_array[c][i]); //enter integers to store in array
}
}
for (i = 0; i != n; i++) {
sum1 += multi_array[i][i]; //add up top left to bottom right diagonal
}
for (i = 0; i != n; i++) {
sum2 += multi_array[i][n-i]; //add up top right to bottom left diagonal
}
printf("%i", abs(sum1 - sum2)); //print absolute value of the difference between diagonals
return 0;
}
Your major problem is here, where you go out of bounds:
for (i = 0; i != n; i++) {
sum2 += multi_array[i][n - i]; // when i is 0, th
}
When i = 0, you are accessing multi_array[0][3], which is out of bounds when N = 3.
So change it to this:
multi_array[i][n - i - 1]
You should read your array like this:
for (i = 0; i < n; i++) {
for (c = 0; c < n; c++) {
scanf(" %d ", &multi_array[i][c]);
}
}
since C stored its arrays in row-major order. What you have stores the array in column-major order. It's not wrong, but it's something you do only if you really have to.
Finally, change again the input part of your code to this:
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (c = 0; c < n; c++) {
scanf("%d", &multi_array[i][c]);
}
}
so that you have to input exactly what you need to. With your initial code I have to type an extra random number when I had completed the input process.
Last but not least, I am posting the whole code, where I have wrote some extra printf()'s, which are actually for the programmer, so that he can see step-by-step if his code is acting as expected or not.
#include <stdio.h>
#include <stdlib.h> /* abs */
int main() {
int n, i, c, multi_array[200][200], sum1 = 0, sum2 = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (c = 0; c < n; c++) {
scanf("%d", &multi_array[i][c]);
}
}
for (i = 0; i < n; i++) {
for (c = 0; c < n; c++) {
printf("|%d|", multi_array[i][c]);
}
printf("\n");
}
for (i = 0; i != n; i++) {
sum1 += multi_array[i][i];
}
printf("sum1 is %d\n", sum1);
for (i = 0; i != n; i++) {
sum2 += multi_array[i][n - i - 1];
}
printf("sum2 is %d\n", sum2);
printf("%i", abs(sum1 - sum2));
return 0;
}
Output:
3
11 2 4
4 5 6
10 8 -12
|11||2||4|
|4||5||6|
|10||8||-12|
sum1 is 4
sum2 is 19
15
You are clearly going out of bounds here:
for (i = 0; i != n; i++) {
sum2 += multi_array[i][n-i]; //add up top right to bottom left diagonal
}
When i is equal to 0 the expression n-i will be equal to n, but the range of the array is from 0 to n-1. The code will read uninitialized values and cause undefined behavior.
The second array index should be 1 less.
I want to store a lower triangular matrix in memory, without storing all the zeros.
The way I have implemented it is by allocating space for i + 1 elements on the ith row.
However, I am new to dynamic memory allocation in C and something seems to be wrong with my first allocation.
int main ()
{
int i, j;
int **mat1;
int dim;
scanf("%d", &dim);
*mat1 = (int**) calloc(dim, sizeof(int*));
for(i = 0; i < dim; i++)
mat1[i] = (int*) calloc(i + 1, sizeof(int));
for(i = 0; i < dim; i++)
{
for(j = 0; j < i + 1; j++)
{
scanf("%d", &mat1[i][j]);
}
}
/* Print the matrix without the zeros*/
for(i = 0; i < dim; i++)
{
for(j = 0; j < (i + 1); j++)
{
printf("%d%c", mat1[i][j], j != (dim-1) ? ' ' : '\n');
}
}
return 0;
}
If you want to conserve space and the overhead of allocating every row of the matrix, you could implement a triangular matrix by using clever indexing of a single array.
A lower triangular matrix (including diagonals) has the following properties:
Dimension Matrix Elements/row Total elements
1 x . . . 1 1
2 x x . . 2 3
3 x x x . 3 6
4 x x x x 4 10
...
The total number of elements for a given dimension is:
size(d) = 1 + 2 + 3 + ... + d = (d+1)(d/2)
If you lay the rows out consecutively in a single array, you can use the formula above to calculate the offset of a given row and column (both zero-based) inside the matrix:
index(r,c) = size(r-1) + c
The formulas above are for the lower triangular matrix. You can access the upper matrix as if it was a lower matrix by simply reversing the indexes:
index((d-1)-r, (d-1)-c)
If you have concerns about changing the orientation of the array, you can devise a different offset calculation for the upper array, such as:
uindex(r,c) = size(d)-size(d-r) + c-r
Sample code:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define TRM_SIZE(dim) (((dim)*(dim+1))/2)
#define TRM_OFFSET(r,c) (TRM_SIZE((r)-1)+(c))
#define TRM_INDEX(m,r,c) ((r)<(c) ? 0 : (m)[TRM_OFFSET((r),(c))])
#define TRM_UINDEX(m,r,c,d) ((r)>(c)?0:(m)[TRM_SIZE(d)-TRM_SIZE((d)-(r))+(c)-(r)])
#define UMACRO 0
int main (void)
{
int i, j, k, dimension;
int *ml, *mu, *mr;
printf ("Enter dimension: ");
if (!scanf ("%2d", &dimension)) {
return 1;
}
ml = calloc (TRM_SIZE(dimension), sizeof *ml);
mu = calloc (TRM_SIZE(dimension), sizeof *mu);
mr = calloc (dimension*dimension, sizeof *mr);
if (!ml || !mu || !mr) {
free (ml);
free (mu);
free (mr);
return 2;
}
/* Initialization */
srand (time (0));
for (i = 0; i < TRM_SIZE(dimension); i++) {
ml[i] = 100.0*rand() / RAND_MAX;
mu[i] = 100.0*rand() / RAND_MAX;
}
/* Multiplication */
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
for (k = 0; k < dimension; k++) {
mr[i*dimension + j] +=
#if UMACRO
TRM_INDEX(ml, i, k) *
TRM_UINDEX(mu, k, j, dimension);
#else
TRM_INDEX(ml, i, k) *
TRM_INDEX(mu, dimension-1-k, dimension-1-j);
#endif
}
}
}
/* Output */
puts ("Lower array");
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
printf (" %2d", TRM_INDEX(ml, i, j));
}
putchar ('\n');
}
puts ("Upper array");
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
#if UMACRO
printf (" %2d", TRM_UINDEX(mu, i, j, dimension));
#else
printf (" %2d", TRM_INDEX(mu, dimension-1-i, dimension-1-j));
#endif
}
putchar ('\n');
}
puts ("Result");
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
printf (" %5d", mr[i*dimension + j]);
}
putchar ('\n');
}
free (mu);
free (ml);
free (mr);
return 0;
}
Note that this is a trivial example. You could extend it to wrap the matrix pointer inside a structure that also stores the type of the matrix (upper or lower triangular, or square) and the dimensions, and write access functions that operate appropriately depending on the type of matrix.
For any non-trivial use of matrices, you should probably use a third-party library that specializes in matrices.
mat1 = calloc(dim,sizeof(int*));
mat1 is a double pointer.You need to allocate memory for your array of pointers and later you need to allocate memory to each of your pointers individually.No need to cast calloc()
You are dereferencing mat1 at line 8 before it has even been set to point anywhere. You are allocating an array of pointers to int, but you are not assigning that to mat1 but to the dereference of mat1, which is uninitialized, we don't know what it points to.
So this line:
// ERROR: You are saying an unknown memory location should have the value of calloc.
*mat1 = (int**)calloc(dim,sizeof(int*));
Should change to:
// OK: Now you are assigning the allocation to the pointer variable.
mat1 = (int**)calloc(dim,sizeof(int*));
I have a program (who's code is available at the bottom) which generates a list of numbers the way that I want it to. The program works fine as is and does exactly what I want. I'm just not a fan of the way I wrote it. It returns "Segmentation fault" if I change one thing, which I'll say at the bottom as well. Here's the code:
#include "stdio.h"
#include "stdlib.h"
#define NCH 81
// Generate swap-mode data for bonds for input.conf file
int main()
{
int i,j,k;
int **dat2, *dat;
//double *dat;
int ns = 500;
int nrow = NCH*(ns-1);
dat = (int*) calloc(nrow, sizeof(int));
dat2 = (int**) calloc(nrow,sizeof(int*));
for (i=0; i<nrow; i++) {
dat2[i] = (int*) calloc(2, sizeof(int));
for (j=0; j<2; j++)
dat2[i][j] = 0;
}
// Generates the bonds
k=2;
for (i=0; i<nrow; i++) {
k--;
for (j=0; j<2; j++) {
dat2[i][j] = k++;
if ( ((k%501) == 0) ) {
k--;
dat2[i][j] = k++;
k++;
}
}
}
FILE *inp2;
inp2 = fopen("bonds.out", "w");
for (i=1; i<=nrow; i++)
fprintf(inp2, "%d %d\n", dat2[i-1][0], dat2[i-1][1]);
fclose(inp2);
// Generates the bond ID in the pattern 1 2 3 3 2 1 ... (appropriate for Bond swap!)
k=1;
while ( k < nrow ) {
for (j=0; j<250; j++) {
dat[k] = (j+1);
k++;
}
for (j=250; j>0; j--) {
dat[k] = j;
k++;
}
}
// Scans bonds.out (because just reporting dat2[][] returns segmentation error, not sure why.
// scans the bonds.out file and stores both values into dm1 and dm2, then reports into 'results.out' file
int dm1, dm2;
FILE *inp;
inp = fopen("input.out", "w");
inp2 = fopen("bonds.out", "r");
for (i=1; i<=nrow; i++) {
fscanf(inp2, "%d %d", &dm1, &dm2);
fprintf(inp, "%d %d %d %d\n", i, dat[i], dm1, dm2);
}
printf("\nDone. All data has been written to \"input.out\"\n");
fclose(inp2);
fclose(inp);
return 0;
}
Now, I don't like the fact that it's first writing dat2[][] to a file and then scanning that file for the values. Why, instead, can't I incorporate dat2[][] in the main loop that writes the "results.out" file? If I do so, I get Segmentation fault. For clarification, I mean changing these lines in the code:
for (i=1; i<=nrow; i++) {
fscanf(inp2, "%d %d", &dm1, &dm2);
fprintf(inp, "%d %d %d %d\n", i, dat[i], dm1, dm2);
}
To these:
for (i=1; i<=nrow; i++) {
fprintf(inp, "%d %d %d %d\n", i, dat[i], dat2[i-1][0], dat2[i-1][1]);
}
I'd love an explanation as I'm still very new to C.
Thanks a lot!
Amit
I think you forgot to subtract 1 from the array index of dat, like this:
for (i=1; i<=nrow; i++) {
fscanf(inp2, "%d %d", &dm1, &dm2);
fprintf(inp, "%d %d %d %d\n", i, dat[i-1], dm1, dm2);
}
The reason why this causes a segfault is because you're looping until i <= nrow, which will go out of the bounds of dat on the final loop iteration.
I think there is something else on your code too besides what Relkin (correctly) pointed out.
int nrow = NCH*(ns-1);
dat = (int*) calloc(nrow, sizeof(int));
If my calculations are right, nrow == 81*(500-1) (== 40419) so it is dat[40419]
On the following explosive algorithm :
k=1;
while ( k < nrow ) {
for (j=0; j<250; j++) {
dat[k] = (j+1);
k++;
}
for (j=250; j>0; j--) {
dat[k] = j;
k++;
}
}
Every internal for loop increases k by 250 (so both increase it by 500 for every while loop), while k < 40419. This means there is a point where k reaches 40001, the condition is satisfied and the loop goes one more. At this moment, on the second for loop you exceed 40419 and write in memory you shouldn't. Take a look at that and check my maths again plz.
Once you are inside the while loop, and the condition falsifies, this does not mean the loop will automatically exit.