[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/diy/ - Do It Yourself


View post   

File: 28 KB, 400x400, 3038900-macro-shot-of-vinyl-player-with-backlight.jpg [View same] [iqdb] [saucenao] [google]
147551 No.147551 [Reply] [Original]

/diy/ someone here knows how to program in ansi c?
I need help with a C program, (ansi c) : Create a Matrix with 20 columns and 10 rows. Create an array with the negative numbers of the matrix. Print the matrix (with full columns).

>> No.147554

Do you know what arrays are?

>> No.147560

You can use the GNU Scientific Library for matrices, but that's probably kinda advanced.

An array is a set of values accessible through an index number. You can use arrays to replicate columns or rows, like this:

row1[] = {1, 2, 3};
row2[] = {4, 5, 6};

>> No.147561

Create an array of [10][20].
>negative numbers of the matrix
what

>> No.147563

>>147561

Oh, right, you can create multidimensional arrays.

>> No.147583

>>147554
Yes. I just don't know how to "separate" the negative numbers and put them in a X array (let's call that variable NEG, lol).

>> No.147592

>>147583
>I just don't know how to "separate" the negative numbers
Check to see if they are less than zero.

If your 2D array is a fixed size of 10 x 20 and can contain any number at each position, then make another array of the same size to hold the negative numbers (since you could have a matrix of entirely negative numbers).

>> No.147609

So you create a separate array that holds a list of negative numbers that were found in the matrix, and then you just print the regular matrix?

Step 1: Create a Matrix by making a 2D array - [10][20] - and fill it with whatever numbers you are supposed to use.

Step 2: Create another array of size 200 or less (potential 200 negative numbers; a dynamic array or even just a vector or stack would be smarter but that's outside the scope of your assignment I assume).
Create a variable for counting.

Step 3: Loop through the array; this takes a nested for loop;
for(iterate through rows)
...for(iterate through columns)
.....if(value is negative)
........store value in array at count variable
........increase count variable

Or something along those lines. Maybe columns first instead of rows? Or maybe not? Try and figure out why one would be better than the other. The count variable is in case you want stats later.

Step 4: Print out the Matrix in a method similar to above. You can use \n and \t to denote newlines and tabs while printing.

The above won't necessarily be right. But it should be able to get you started.