enter image description here
Hello, I was coding for cs50 in the mario problem but it seems that there is an error that says:
^
mario.c:20:26: error: use of undeclared identifier 'k'
for (k = 1; k <= n - i; k++){
^
3 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
Why is this so? :/
This is because you haven't declared the variable k anywhere.
Usually, in a for loop, the iterator variable is declared at the start. For example,
for (int i = 0; i < 10; i++)
{
// code to run
}
In your code, you haven't declared 'k' by explicitly stating int k = 0; Hence, to fix your code, try adding an int (to declare the variable k as an int) before the first k.
Related
I have the following circuit in Circom cicuit compiler:
pragma circom 2.0.0;
template MAIN() {
signal len;
len <== 32;
for (k = 0; k < maplen; k++) {
// do something
}
}
component main = MAIN();
I'm getting an error:
error[T2005]: Typing error found
┌─ "/Users/ilia/compiling/main-circom/circuits/main.circom":118:17
│
118 │ for (k = 0; k < len; k++) {
│ ^^^^^^^ There are constraints depending on the value of the condition and it can be unknown during the constraint generation phase
How do I write this loop in a way which makes it possible to iterate len times where len is a signal?
You need to decide how many maximum iterations your loop can have (an iteration budget) and then discard all the iterations you don't need using LessThan component. You can also select the needed result of a iteration using QuinSelector
I'm making a cryptographer and as a step trying to turn the string into char to be able to replace each char with a "crypto char". However I can't get this code to work and I have no idea what the problem is.
My code:
String encrypt(String text) {
int strLength = text.length();
for (int k=0, k < strLength, k++) {
letter = text.charAt(k);
}
}
I'm getting these errors:
Syntax error, insert "; ; ) Statement" to complete ForStatement
Duplicate local variable k
Syntax error on token(s), misplaced construct(s)
k cannot be resolved to a variable
Syntax error on token ")", ; expected
k cannot be resolved to a variable
at inl1.Cryptographer.encrypt(Cryptographer.java:25)
Line 25 is the "for" line.
Help is very appreciated!
I don't speek Java, but I think your for cycle should use semicolons ; instead of commas , :-)
for (int k=0; k < strLength; k++) {
...
}
I have been trying to make my pyramid from left aligned to right aligned but i am confused on how to do it. This is the code i am using.
Edit: i have changed the code but i have been getting an error
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
//asks user for number between 1 and 8
height = get_int("please give height: ");
}
while (height < 1 || height > 8);
//prints rows (i)
for (int rows = 0; rows < height; rows++)
{
//prints spaces (j)
for (int spaces = 0; spaces < height - rows; spaces++)
{
printf(".");
}
printf("\n");
}
for (int hashes = 0; rows < height - rows; hashes++)
{
printf("#");
}
printf("\n");
}
user gets prompted and writes number between 1 and 8
user types 4
Expected
...#
..##
.###
####
Actual output
....
...
..
.
mario.c:24:48: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
mario.c:24:32: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
i am trying to print hashes and use the rows interger but for some reason the error says it is am undefined interger.
I think the error is because you're curly brackets {} in your first and second for loops are mixed up. It looks like you're trying to do three loops that are nested inside each other; however, your third loops is outside the first one because you have an extra } in the middle of your code for the second loop. The variable row is declared in the first loop and the third loop doesn't know what that means since it is outside the first loop.
Sticking to the class's recommendations about indentation helped me keep this straight.
I am trying to build a specific matrix but using simply R can take a lot of time considering the size of the entries that I have to use. I write a function in Rcpp with the Armadillo functionality because I need the linear algebra part to work with matrices. My code is the next:
library('Rcpp')
library('inline')
library('RcppArmadillo')
cppFunction("arma::mat GramMat(arma::mat A, double parametro, int n) {
arma::mat resultado=A;
double temp;
for (int i=0; i<n; i++){
for (int j=i; j<n; j++){
resultado(j,i)= exp(-1*parametro*((A.col(i)-A.col(j)).t() * (A.col(i)-A.col(j))));
}
}
for (int i=0; i<n; i++){
for (int j=0; j<i; j++){
resultado(i,j)=resultado(j,i);
}
}
return resultado;}",depends="RcppArmadillo")
and I am getting the next error:
temp= exp(-1*parametro*((A.col(i)-A.col(j)).t() * (A.col(i)-A.col(j))));
^
make: *** [file548914af6578.o] Error 1
The problem is with the assignation, because I tried assigning just a 1 and the assignation is working well. And I tought that maybe the problem was with the right hand side but I print it with Rcout and is delivering well number.
When I tried compiling your code, I saw a more informative error message:
file2f78133e7bc2.cpp: In function ‘arma::mat GramMat(arma::mat,
double, int)’: file2f78133e7bc2.cpp:14:99: error: cannot convert
‘arma::enable_if2,
arma::subview_col, arma::eglue_minus>, arma::op_htrans>,
arma::eGlue, arma::subview_col,
arma::eglue_minus>, arma::glue_times>, arma::eop_scalar_times>,
arma::eop_exp> >::result {aka const
arma::eOp,
arma::subview_col, arma::eglue_minus>, arma::op_htrans>,
arma::eGlue, arma::subview_col,
arma::eglue_minus>, arma::glue_times>, arma::eop_scalar_times>,
arma::eop_exp>}’ to ‘double’ in assignment
resultado(j,i)= exp(-1*parametro*((A.col(i)-A.col(j)).t() * (A.col(i)-A.col(j))));
^ make: *** [file2f78133e7bc2.o] Error 1
This leads us directly to the problem; the operation
(A.col(i)-A.col(j)).t() * (A.col(i)-A.col(j))
returns a type that cannot be directly converted to a double. However, we can just use arma::as_scalar() to fix this (see here in the Armadillo documentation); the following compiled fine for me:
cppFunction("arma::mat GramMat(arma::mat A, double parametro, int n) {
arma::mat resultado=A;
double temp;
for (int i=0; i<n; i++){
for (int j=i; j<n; j++){
resultado(j,i)= arma::as_scalar(exp(-1*parametro*((A.col(i)-A.col(j)).t() * (A.col(i)-A.col(j)))));
}
}
for (int i=0; i<n; i++){
for (int j=0; j<i; j++){
resultado(i,j)=resultado(j,i);
}
}
return resultado;}",depends="RcppArmadillo")
There are quite a few other things that could be improved in this code, of course. For example, as Dirk Eddelbuettel points out, you actually never use temp in your code. You might also want to use arma::dot() to get the dot product of (A.col(i)-A.col(j)) with itself (see here in the Armadillo documentation -- as arma::dot() returns a double, it would also eliminate the need to use arma::as_scalar()), etc.
I am using the following code to divide all int array elements with constant factor using SSE.
void sse_div(int *arr,int num_shift,int N) // devide all array elements by 2
{
num_shift=1;
int nb_iters = N / 4;
__declspec(align(32))int *a1=arr;
__m128i* l = (__m128i*)a1;
for (int i = 0; i < nb_iters; ++i, ++l)
_mm_store_si128( l, _mm_srai_epi32(*l,num_shift)); //Error line
}
But I am getting the following error
I am unable to get rid of this problem.
Can anybody please help to solve this problem.
Any help will be appreciated.
Thanks in Advance
Since your input array is apparently misaligned you can use unaligned loads/stores, e.g.:
void sse_div(int *arr, int N) // divide all array elements by 2
{
for (int i = 0; i < nb_iters; i += 4)
{
__m128i v = _mm_loadu_si128(&arr[i]);
v = _mm_srai_epi32(v, 1);
_mm_storeu_si128(&arr[i], v);
}
}
Note that there may be a significant performance hit from using unaligned loads/stores (depending on what CPU you are running on), so if possible you should make your arr array 16 byte aligned when you allocate the memory.