code for arr

on the part not working,
i want to disp
1 12 23 0
-1 -12 -23 289

but it show
-1 -1 35 0
0 0 35 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<math.h>
#define ARRLENG 8
#define ARRCOL 4
#define ARRROW 2
int main()
{
	int i, r, myArr[ARRLENG]={1, 12, 23, 0, -1, -12, -23, 289}, my2DArr[ARRROW] [ARRCOL];
	
	printf("The Data in my Arr are:\n");
	
	
	for (i=0; i<8; i++)
	{
	
		printf("%d ",myArr[i]);	
	}
	printf("\n\nThe Data in my2DArr are\n");


	/*this part not working*/
	for (r=0; r<4; r++)
	{
		myArr[r]= my2DArr[1] [r];
		printf("%3d ", my2DArr[1] [r]);
	}
	
	printf("\n");
	
	for (r=4; r<8; r++)
	{
		myArr[r]= my2DArr[2] [r];
		printf("%3d ", my2DArr[2] [r]);
	}
	
int mynewarr[ARRLENG];

printf("\n\n\nEnter 8 integers for mynewarr:\n\n");

scanf("%d%d%d%d%d%d%d%d", &(mynewarr[1]),&(mynewarr[2]), &(mynewarr[3]),&(mynewarr[4]),&(mynewarr[5]), &(mynewarr[6]),&(mynewarr[7]), &(mynewarr[8]));
	
		for (i=0; i<8; i++)
	{
	
		printf("%d ",mynewarr[i]);	
	}
	return 0;

}
Last edited on
1
2
3
4
5
6
\*this part not working*\
	for (r=0; r<4; r++)
	{
		myArr[r]= my2DArr[1] [r];
		printf("%3d ", my2DArr[1] [r]);
	}


You're outputting from my2DArr, which contains random garbage data. You have to set some data in my2DArr first.

Perhaps this line is your mistake:
myArr[r]= my2DArr[1] [r];
Last edited on
my2DArr[1] [r]= myArr[r];

some how this work
Well yes. That's how C++ works.

When dealing with simple int values, A = B means "make the int on the left the same value as the int on the right".

Let's work through an example:

1
2
3
4
int x = 3;  // x is 3
int y = 4;  // y is 4

x = y;


What's the value of x now? 4
What's the value of y now? 4

You definitely need to make sure you understand this. Setting values is a really fundamental part of C++.

Last edited on
Topic archived. No new replies allowed.