how to multiply integer or double with values in an array? - c

In a simple way, i have an array of 10 values for example..and i would like to multiply each value with 5. Can i actually just do the following?
for (i = 0 ; i <10 ; i++)
{
x[i]=x[i]*5;
}
And what about getting square for values in the array and be stored back into the same array? As in I want x[i]=x[i]*x[i].
Can I actually just do the multiplication like that?
I tried a couple of combination but it didnt really work..hope someone can help out! Thanks!

This is perfectly fine.

edited now that the language has been specified
Yes, this "just works" in C.
Can you post more about the errors you're seeing so that we can try to help you out?

Related

(C) Minesweeper calculating function goes wrong

Hope your day going well. Thank you for visiting my post.
So I was trying to build minesweeper game, but got stuck at some problem.
Long story short, I have 2D array size 8*8. There are randomally 8 mines around the array.
I want to check: if at [row][col] there is no mine, whats the INT value it should hold.
I wrote a function that checks all the 8 options around the specific [row][col] I have.
For example:
if (game[row-1][col-1] == '*')
counter++
if (game[row][col+1] == '*')
counter++
and so on... 8 times. At the end, the function returns the counter value.
So I got stuck when I was trying to check, for example, row = 0 col = 7.
Because, row-1 gets me back to row[7], and col+1 gets me to col[0], and I don't want any of this to happen.
I've been sitting about 5 hours today, thinking of a way to fix this. But I couldn't think of any possibility rather than (approximately) 23 if's in one function.
Can anyone PLEASE hint me which steps I should take?
Thanks in advance!! :)

AND array formula

I know AND doesn't work as an array formula.
But does anyone know how could I make something like this with out the AND?
=ArrayFormula(AND(M2:M="CAD data",AG2:AG<>""))
use multiplication:
=ARRAYFORMULA((M2:M="CAD data")*(AG2:AG<>""))

scala 2.11.8 how to fill an array

I want to create an array that contains the same value repeated for a very large number of times, say 1,000,000.
I was thinking to use something like Array.fill(1000000)(0). However, after reading the documentation for Scala 2.11.8, I found that there is no such members of Array in this version.
Is there any other ways that can create the array without using loop? Thanks in advance for your help.
This will do the trick:
Array.fill[Int](1000000)(0)
Read more here: https://alvinalexander.com/scala/scala-list-class-examples
You can use range to iterate through required length (1000000 times in your case) and then return a default value which is 0 in each iteration as below.
val arr:Array[Int] = (1 to 1000000 map(_ => 0)).toArray
Stream.continually(0).take(1000000).toArray would do that .. but why in the world would you want something like this???

Indexing ones into a matrix of zeros without loops

I'm fairly new to MATLAB and I need some help with this problem.
the problem is to write a function that creates an (n-n) square matrix of zeros with ones on the reverse diagonal
I tried this code:
function s=reverse_diag(n)
s=zeros(n);
i=1;j=n;
while i<=n && j>=1
s(i,j)=1;
i=i+1;
j=j-1;
end
but I want another way for solving it without using loops or diag and eye commands.
Thanks in advance
The easiest and most obvious way to achieve this without loops would be to use
s=fliplr(eye(n))
since you stated that you don't want to use eye (for whatever reason), you could also work with sub2ind-command. It would look like this:
s=zeros(n);
s(sub2ind(size(s),1:size(s,1),size(s,2):-1:1))=1

How to determine odd number from a displayed sequence in C?

I would like to determine the odd numbers from a given sequence of numbers. After determining,I want to display those odd numbers.
Can you help me?
I can only use loops,if-elseif-else,swtich,break and continue. Anything beyond the said lessons are not allowed.
Thank you.
P.S
I am sorry if I cannot provide the code. I want to code it on my own.I just need some ideas from you.
Steps:
Loop through the array(sequence) element by element
If an array_element%2 is one, display the number.(% is the modulus operator and gets the remainder of the division of its operands)
Now just convert the above steps into code.

Resources