Here is my code:
int main() {
int x, y;
int *xptr, *yptr;
int array[10][10];
int j;
int k;
int z = 0;
for(j = 0; j < 10; j++) {
for(k = 0; k < 10; k++) {
array[j][k] = j * 10 + k;
}
}
xptr = &array[0][0];
for(j = 0; j < 10; j++) {
for(k = 0; k < 10; k++) {
printf("array[%d][%d] = %d \n", j, k, *(xptr + j), (xptr + k));
}
}
system("PAUSE");
}
I am trying to initialize a 2d array so that at [0][0] it equals 0 and at [9][9] it equals 99. With the way that it is now, [0][0-9] all equal 0 and then [1][0-9] all equal 1. How would I properly load this array in the fashion that I mentioned?
for(j = 0; j < 10; j++) {
for(k = 0; k < 10; k++) {
array[j][k] = j*10 + k;
}
}
I'm assuming you've actually declared everything, but didn't include it in the example. You simply want
array[j][k] = j*10 + k;
Related
Im trying to sort a matrix by the sum of its row's digits, from highest to lowest. I dont know if i explained that correctly so here's some photos explaining it.
This is what my code outputs. Basically, it asks you for m and n, which are the dimensions of the matrix. In this example it's a 3x4, 3 rows and 4 columns. Then, the matrix should be sorted by rows, by the sum of row's digits. Which means, instead of what's being outputted in the picture above, the correct result should be this:
I have no idea how to sort this from highest to lowest, i have been trying for hours to no avail.
Here's my code:
#include <stdio.h>
#define N 30
void main(){
double a[N][N], s[N], p;
int i, j, m, n, max;
while(1){
printf("\nm, n? ");
scanf("%d%d", &m, &n);
if(m <= 0 || m > N || n <=0 || n > N)
break;
for(i = 0; i < m; i++){
printf("%2d. row? ", i+1);
for(j = 0; j < n; scanf("%lf", &a[i][j++]));
}
for(i = 0; i < m; i++)
for(s[i] = j = 0; j < n; s[i] += a[i][j++]);
for(j = 0; j < n - 1; j++){
for(max = i, j = i+1; j < n; j++)
if(s[j] > s[max])
max = i;
if(max != j){
p = s[j];
s[j] = s[max];
s[max] = p;
for(j = 0; j < m; j++){
p = a[j][i];
a[j][i] = a[j][max];
a[j][max] = p;
}
}
}
printf("New matrix: \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for(j = 0; j < m; j++)
printf("-------------");
printf("\n");
for(j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
}
You can sort the rows of the matrix from highest to lowest, using a simple bubble sort algorithm.Your code modified below:
int main() {
double a[N][N], s[N], p;
int i, j, m, n, max;
while (1) {
printf("\nm, n? ");
scanf("%d%d", & m, & n);
if (m <= 0 || m > N || n <= 0 || n > N)
break;
for (i = 0; i < m; i++) {
printf("%2d. row? ", i + 1);
for (j = 0; j < n; scanf("%lf", & a[i][j++]));
}
for (i = 0; i < m; i++)
for (s[i] = j = 0; j < n; s[i] += a[i][j++]);
for (i = 0; i < m - 1; i++) { // modified here
for (j = i + 1; j < m; j++) { // modified here
if (s[j] > s[i]) { // modified here
p = s[i];
s[i] = s[j];
s[j] = p;
for (int k = 0; k < n; k++) {
p = a[i][k];
a[i][k] = a[j][k];
a[j][k] = p;
}
}
}
}
printf("New matrix: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for (j = 0; j < m; j++)
printf("-------------");
printf("\n");
for (j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
return 0;
}
Here's how i modified your code to achieve that:
Initialize a loop variable i to 0.
In the outer loop, run the inner loop j from i+1 to m-1.
In the inner loop, compare the sum of the row i with the sum of row
j. If the sum of row j is greater than the sum of row i, swap the
rows using a temporary variable.
After the inner loop finishes, increment the value of i by 1. Repeat
the outer loop until i becomes equal to m-1.
Output:
You can just use qsort to let it handle the sorting and item swapping. Then you only need to write the code for comparing two rows with each other.
Given something like this:
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
You'd call qsort as:
qsort(matrix, 3, sizeof(int[4]), compare);
The only complexity is implementing the comparison callback function. There's two things to consider there:
We've told qsort that we have an array of 3 items, each of type int[4]. So the void pointers it passes along to us will actually be pointers to type int[4]. That is: int(*)[4].
qsort sorts in ascending order by default, where the item considered "less" ends up first. So we need to tweak that to get the largest item first.
Example:
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
sum1 < sum2 would have placed the smallest row first.
Full example:
#include <stdio.h>
#include <stdlib.h>
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
void print_matrix(size_t col, size_t row, int matrix[col][row])
{
for(size_t i=0; i<col; i++)
{
for(size_t j=0; j<row; j++)
{
printf("%d,", matrix[i][j]);
}
puts("");
}
}
int main (void)
{
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
print_matrix(3,4,matrix);
puts("");
qsort(matrix, 3, sizeof(int[4]), compare);
print_matrix(3,4,matrix);
}
I'm trying to write a programm that solves system of equations Ax=B using Gauss-Jacobi iteration method.
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
double **a, *b, *x, *f, eps = 1.e-2, c;
int n = 3, m = 3, i, j, bool = 1, d = 3;
/* printf("n=") ; scanf("%d", &n);
printf("m=") ; scanf("%d", &n) */
a =malloc(n * sizeof *a);
for (i = 0; i < n; i++)
a[i] = (double*)malloc(m * sizeof(double));
b = malloc(m * sizeof *b);
x = malloc(m * sizeof *x) ;
f = malloc(m * sizeof *f) ;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("a[%d][%d]=", i, j);
scanf("%le", &a[i][j]);
if(fabs(a[i][i])<1.e-10) return 0 ;
}
printf("\n") ;
}
printf("\n") ;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("a[%d][%d]=%le ", i, j, a[i][j]);
}
printf("\n") ;
}
for (j = 0; j < m; j++) {
printf("x[%d]=", j);
scanf("%le", &x[j]);
} //intial guess
printf("\n") ;
for (j = 0; j < m; j++) {
printf("b[%d]=", j);
scanf("%le", &b[j]);
}
printf("\n") ;
while (1) {
bool = 0;
for (i = 0; i < n; i++) {
c = 0.0;
for (j = 0; j < m; j++)
if (j != i)
c += a[i][j] * x[j];
f[i] = (b[i] - c) / a[i][i];
}
for (i = 0; i < m; i++)
if (fabs(f[i] - x[i]) > eps)
bool = 1;
if (bool == 1)
for (i = 0; i < m; i++)
x[i] = f[i];
else if (bool == 0)
break;
}
for (j = 0; j < m; j++)
printf("%le\n", f[j]);
return 0;
}
The condition of stoping the loop is that previous approximation minus current approximation for all x is less than epsilon.
It seems like i did everything according to algorithm,but the programm doesn't work.
Where did i make a mistake?
While not the most strict condition, the usual condition requiered to guarantee convergence in the Jacobi and Gauss-Seidel methods is diagonal dominance,
abs(a[i][i]) > sum( abs(a[i][j]), j=0...n-1, j!=i)
This test is also easy to implement as a check to run before the iteration.
The larger the relative gap in all these inequalities, the faster the convergence of the method.
I know that object oriented is not welcoming in c language but I still trying to work my way because it is possible as later languages based on c works good with objects
so my question is when I try to write a function outside of main it doesnt give me access and doesnt change values of wanted structs , see code below (I marked every things and included working functions inside the main) :
#include <stdio.h>
#include <stdlib.h>
static int RandomNum() {
int generate = rand() / 100000000;
while (generate >= 9) {
generate = rand() / 100000000;
}
return generate;
}
// Object Definition
typedef struct {
int a, b;
int Mat[2][4];
} Matrix2x4;
typedef struct {
int a, b;
int Mat[4][5];
} Matrix4x5;
typedef struct {
int a, b;
int Mat[4][5];
} Matrix2x5;
void PrintMat2x4(Matrix2x4 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
void PrintMat4x5(Matrix4x5 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
void PrintMat2x5(Matrix2x5 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
// NOT WORKING AS A SEPERATE FUNCTION SO I PUT IT INSIDE THE MAIN
/*static void MatrixMultiplication (Matrix2x4 a , Matrix4x5 b , Matrix2x5 c) {
for (int i = 0; i < a.b; i++){
for (int j = 0; j < b.b; j++){
for (int k = 0; k < a.b; k++){
c.Mat[i][j] = c.Mat[i][j]+(a.Mat[i][k]*b.Mat[k][j]);}}}
}
void setRandom (Matrix a) {
int row = ((int)(sizeof (a.Mat) / sizeof (a.Mat)[0])) ;
int col = ((int)(sizeof (a.Mat)[0] / sizeof (a.Mat)[0][0])) ;
for (int i = 0; i < row ; i++){
for (int j = 0; j < col; j++){
a.Mat[i][j] = RandomNum();}}}*/
void main() {
printf("%s\n\n", "Start..");
Matrix2x4 test = {2,4,{0}}; // <----- SIZE IS 2X4
int row = ((int)(sizeof(test.Mat) / sizeof(test.Mat)[0]));
int col = ((int)(sizeof(test.Mat)[0] / sizeof(test.Mat)[0][0]));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
test.Mat[i][j] = RandomNum();
}
}
Matrix4x5 test2 = {4,5,{0}}; // <----- SIZE IS 4X5
int row2 = ((int)(sizeof(test2.Mat) / sizeof(test2.Mat)[0]));
int col2 = ((int)(sizeof(test2.Mat)[0] / sizeof(test2.Mat)[0][0]));
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
test2.Mat[i][j] = RandomNum();
}
}
Matrix2x5 mult = {2,5,{0}}; // <----- SIZE IS 2X5
PrintMat2x4(test);
printf("X\n");
PrintMat4x5(test2);
printf("=\n");
for (int i = 0; i < test.b; i++) {
for (int j = 0; j < test2.b; j++) {
for (int k = 0; k < test.b; k++) {
mult.Mat[i][j] = mult.Mat[i][j] + (test.Mat[i][k] * test2.Mat[k][j]);
}
}
}
PrintMat2x5(mult);
printf("\n\n%s", "End ---> ");
}
In order for this function to work:
static void MatrixMultiplication (Matrix2x4 a , Matrix4x5 b , Matrix2x5 c) {
for (int i = 0; i < a.b; i++){
for (int j = 0; j < b.b; j++){
for (int k = 0; k < a.b; k++){
c.Mat[i][j] = c.Mat[i][j]+(a.Mat[i][k]*b.Mat[k][j]);}}}
}
You must pass your structs as pointers:
static void MatrixMultiplication (Matrix2x4* a , Matrix4x5* b , Matrix2x5* c) {
for (int i = 0; i < a->b; i++){
for (int j = 0; j < b->b; j++){
for (int k = 0; k < a->b; k++){
c->Mat[i][j] = c->Mat[i][j]+(a->Mat[i][k]*b->Mat[k][j]);}}}
}
Your original function doesn't work outside of main, because C is making a copy of those structs for the function's own use. Therefore the original structs never get modified. Changing those parameters to pointers means that the function is working with the original structs.
I have taken two 2d arrays but the output is very different from expected.It should merge two 2d arrays into one 1d array.I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] = array1[i-3][j];
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
Please try this code,To Merge two 2d arrays into one 1d array in C
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? (arraySum[i][j] = array2[i][j]) : (arraySum[i][j] = array1[i-3][j]);
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I hope this code will be usefull.
Thank you.
I believe you're trying to be too smart with ternary operator, you can do it simpler way:
if (i < 3)
arraySum[i][j] = array2[i][j];
else
arraySum[i][j] = array1[i-3][j];
Listen to your compiler it would've told you what was wrong if you've compiled with -Wall -Wextra.
And if you insist on using ternary then this would probably be clearer:
arraySum[i][j] = (i < 3) ? array2[i][j] : array1[i-3][j];
I'm working on my class project and I'm currently stuck at the most basic one. Basically I have to fill the stack of boxes using loops and 3d array. The stack is 4 width, 4 length and 3 height and I have to fill boxes with 100 items each.
void main(){
int boxShleve[3][4][4];
int i, j, k;
for (i=0; i<3; ++i){
for (j=0; j<4; ++j){
for (k=0; k<4; ++k){
boxShleve[3][4][4] = 100;
}
}
}
printf("%d", boxShleve[3][4][4]);
}
This is the broken piece of my work... How do I make each array has 100 element in it?
This is what you meant to do:
int main()
{
int boxShleve[3][4][4];
int i, j, k;
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
for (k = 0; k < 4; ++k)
boxShleve[i][j][k] = 100;
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
for (k = 0; k < 4; k++)
printf("%d ", boxShleve[i][j][k]);
return 0;
}
The reason you need the nested loops is to use the i, j and k as indexes to access the array. So you have to use them.
Same for printing the values.
A faster way to do this if you are using GCC is as follows.
int boxShleve[3][4][4] = {
{[0 ... 2] = 100 },
{[0 ... 3] = 100 },
{[0 ... 3] = 100 } };
#include <stdio.h>
int main(){
int boxShleve[3][4][4];
size_t size = sizeof(boxShleve)/sizeof(int);
int *p = &boxShleve[0][0][0];
while(size--)
*p++ = 100;
printf("%d\n", boxShleve[2][3][3]);//last element,
return 0;
}