问:
When I try to destructure my state object in react typescript, I get an `Object is possibly 'undefined'` error
When I try to destructure my state object in react typescript, I get an Object is possibly 'undefined' error. If I use optional chaining I got this error const newUser: NewUser | undefined Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.

The error is showing when I am trying to destructure object RegisterUser(newUser?.email, newUser?.password, newUser?.name, navigate)

My Code:

```<code>type NewUser ={
    name: string,
    email: string,
    password: string,
    navigate: string
}

const Register = () => {
    const [newUser, setNewUser] = useState<NewUser>() // single object will return

    const [RegisterUser] = UseFirebase()
    const navigate = useNavigate()

    const handleValues = (e: any) => {
        const field = e.target.name;
        const value = e.target.value
        const user: any = { ...newUser }
        user[field] = value
        setNewUser(user)

    }

    const handleRegister = (e: any) => {
        e.preventDefault()
        // ERROR
        RegisterUser(newUser.email, newUser.password, newUser.name, navigate)
        // console.log('user name', newUser.name, newUser.email, newUser.password);
    }
.......
</code>```

Could someone please tell me what type of mistake I made?

答:
Well that the typescript for yaa, you need to check first that you will not get undefined as a value by simply doing condition check like this</p>
```<code>const handleRegister = (e: any) => {
        e.preventDefault()
        if (newUser?.email && newUser?.password && newUser?.name) {
        RegisterUser(newUser.email, newUser.password, newUser.name, navigate)
        } else {
// show some error handling here or error to user that he didnt enter value etc
 }
    }
</code>```

Body:
When I try to destructure my state object in react typescript, I get an Object is possibly 'undefined' error. If I use optional chaining I got this error const newUser: NewUser | undefined Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.

The error is showing when I am trying to destructure object RegisterUser(newUser?.email, newUser?.password, newUser?.name, navigate)

My Code:

type NewUser ={
    name: string,
    email: string,
    password: string,
    navigate: string
}

const Register = () => {
    const [newUser, setNewUser] = useState<NewUser>() // single object will return

    const [RegisterUser] = UseFirebase()
    const navigate = useNavigate()

    const handleValues = (e: any) => {
        const field = e.target.name;
        const value = e.target.value
        const user: any = { ...newUser }
        user[field] = value
        setNewUser(user)

    }

    const handleRegister = (e: any) => {
        e.preventDefault()
        // ERROR
        RegisterUser(newUser.email, newUser.password, newUser.name, navigate)
        // console.log('user name', newUser.name, newUser.email, newUser.password);
    }
.......

Could someone please tell me what type of mistake I made?

Body:
Well that the typescript for yaa, you need to check first that you will not get undefined as a value by simply doing condition check like this

const handleRegister = (e: any) => {
        e.preventDefault()
        if (newUser?.email && newUser?.password && newUser?.name) {
        RegisterUser(newUser.email, newUser.password, newUser.name, navigate)
        } else {
// show some error handling here or error to user that he didnt enter value etc
 }
    }


来源: stackexchange

问:
Triplet Codility in C - getting only 11 % (Training)
I am quite new to C and learning it partly also by going through Codility training.

For the triplet question, however I am only getting 11 % I am not sure what is wrong here. The question is: A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).

Your goal is to find the maximal product of any triplet.

Write a function:

```<code>int solution(int A[], int N);
</code>```

that, given a non-empty zero-indexed array A, returns the value of the maximal product of any triplet.
For example, given array A such that:

```<code>A[0] = -3
A[1] = 1
A[2] = 2
A[3] = -2
A[4] = 5
A[5] = 6
</code>```

the function should return 60, as the product of triplet (2, 4, 5) is maximal.

Assume that:
•N is an integer within the range [3..100,000];
 •each element of array A is an integer within the range [−1,000..1,000].

Complexity:
•expected worst-case time complexity is O(N*log(N));
 •expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

My Code which gives me 11 % is, I want to know where I am going wrong with this in this code. I first sort the matrix and then compare the three largest positive numbers and 2 largest negative together with largest positive:

```<code>int solution(int A[], int N) {
    int i,j,PQR_pos,PQR_neg, temp;

    for (i=0; i<N; i++) {
        for (j=0; j<N-i; j++)
            if (A[j+1] < A[j]) { /* compare the two neighbours */
                temp = A[j]; /* swap a[j] and a[j+1] */
                A[j] = A[j+1];
                A[j+1] = temp;
            }
    }

    PQR_pos = A[N] * A[N-1] * A[N-2];
    PQR_neg = A[N] * A[1] * A[0];

    if (PQR_pos>PQR_neg) return PQR_pos;
    else return PQR_neg;

}
</code>```

答:
All, I am thinking,
(0 ≤ P < Q < R < N). this condition is just a trap.
The main goal of the solution is to find the top 3 highest values in the array, the sequence will be arranged automatically.

Guys, this is the solution I found, using Javascript Code.

```<code>// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    A.sort((a,b)=>a-b);
    //console.log("A",A);

    let p = A[A.length-3];
    let q = A[A.length-2];
    let r = A[A.length-1];

    return p*q*r;

}
</code>```

Have a happy learning.
Cheers.

ContentLicense: CC BY-SA 3.0

Body:
I am quite new to C and learning it partly also by going through Codility training.

For the triplet question, however I am only getting 11 % I am not sure what is wrong here. The question is: A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).

Your goal is to find the maximal product of any triplet.

Write a function:

int solution(int A[], int N);

that, given a non-empty zero-indexed array A, returns the value of the maximal product of any triplet.
For example, given array A such that:

A[0] = -3
A[1] = 1
A[2] = 2
A[3] = -2
A[4] = 5
A[5] = 6

the function should return 60, as the product of triplet (2, 4, 5) is maximal.

Assume that:
•N is an integer within the range [3..100,000];
 •each element of array A is an integer within the range [−1,000..1,000].

Complexity:
•expected worst-case time complexity is O(N*log(N));
 •expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

My Code which gives me 11 % is, I want to know where I am going wrong with this in this code. I first sort the matrix and then compare the three largest positive numbers and 2 largest negative together with largest positive:

int solution(int A[], int N) {
    int i,j,PQR_pos,PQR_neg, temp;

    for (i=0; i<N; i++) {
        for (j=0; j<N-i; j++)
            if (A[j+1] PQR_neg) return PQR_pos;
    else return PQR_neg;

}

Body:
All, I am thinking,
(0 ≤ P < Q < R < N). this condition is just a trap.
The main goal of the solution is to find the top 3 highest values in the array, the sequence will be arranged automatically.

Guys, this is the solution I found, using Javascript Code.

// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    A.sort((a,b)=>a-b);
    //console.log("A",A);

    let p = A[A.length-3];
    let q = A[A.length-2];
    let r = A[A.length-1];

    return p*q*r;

}

Have a happy learning.
Cheers.

ContentLicense: CC BY-SA 4.0


问:
IIS Long operation request and performance issue
just wondering
if I have a webpage that generates a pdf, but could take a while to generate due to long sql request and number of data to insert and tread before generating pdf.
If the request hasn't finished yet and the user seeing that nothing happened clicks again on button and again and again.
What will happen in my web application and in the database?
Is it going to wait for previous request to be finished before throwing another one?
Does it accept multiple requests per session?
Is my web application going to freeze?
Is my database going to performe multiple sql request at the same time from same user?
Is my sql server going to freeze?

I know I should not leave it like that and make the button unclickable and put a little message "Please wait" but I'm just interested in what would happen in that situation.

Sorry for bad english!
Thanks!

答:
Check out this answer:  Problem with IHttpAsyncHandler and ASP.NET "Requests Executing" counter for an technical viewpoint.

You had a number of questions:

If the request hasn't finished yet and the user seeing that nothing happened clicks again on button and again and again?

In this case, multiple requests will be queued on your web server, and they will all be processed.  This will affect performance.  As you mentioned, your UI should prevent this by disabling the button and giving the user some feedback that the request is being processed.

What will happen in my web application and in the database? Is it going to wait for previous request to be finished before throwing another one?

No.  Unless you are locking on a single resource in your database, each request will be handled by a separate worker in IIS.  There are limits to the number of concurrent requests, but generally, things will happen in parallel.  If the work you are doing is CPU intensive, there could be some contention for CPU resources and overall performance will suffer.  You should definitely look in to the AsyncHandler model.

Does it accept multiple requests per session?

Yes

Is my web application going to freeze?

Freeze might be the wrong word.  It is possible that the queue for requests will grow large if requests are coming in at a rate faster than the web server can fulfill them.  If this happens, it will appear to users that your web server is unresponsive or slow.

Is my database going to performe multiple sql request at the same time from same user?

It might.

Is my sql server going to freeze?

Same as above.  If your SQL Server cannot process requests faster than they are coming in, it may appear to your users that your application is unresponsive.


问:
How can I make a buttons from a Layout in Android Studio
I have buttons what I made with "LinearLayout" but when I want to made them clickable and so that it goes to another activity. I understand, that I need to change from "LinearLayout" to "ImageButton" because I want to make a beautiful app [what I want it looks like][1], but when I launch it, it stops working. What can I do to repair that? Thank you in advance for your help and sorry for my bad english. :(</p>
```<code><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/header_background"
        android:padding="20dp"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textHello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@string/hello"
            android:textColor="@color/colorWhite"
            android:textSize="25sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="TextContrastCheck" />

        <TextView
            android:id="@+id/textUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@string/vassili"
            android:textColor="@color/colorWhite"
            android:textSize="25sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/textHello"
            tools:ignore="TextContrastCheck" />

        <ImageView
            android:id="@+id/imageMenu"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_menu"
            app:layout_constraintBottom_toBottomOf="@id/textHello"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/textHello"
            app:tint="@color/colorWhite"
            tools:ignore="ImageContrastCheck" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layoutHeader"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="-96dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/layoutGoal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toStartOf="@id/layoutTraining"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/notepad" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/goals"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutTraining"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/layoutGoal"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/dumbbell" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Trainings"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutWater"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toStartOf="@id/layoutTraining"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layoutGoal"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/bottle" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Water"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutStepCounter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/layoutGoal"
                app:layout_constraintTop_toBottomOf="@id/layoutTraining"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/walk" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/step_counter"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

  [1]: https://i.stack.imgur.com/WdW6v.png
</code>```

答:
For this type of layout you need to make your LinearLayout clickable
For example:

```<code>LinearLayout linearLayoutGoals;
</code>```

then in you onCreate do findViewById and set its clickListener

```<code>linearLayoutGoals = findViewById(R.id.layoutGoal);
linearLayoutGoals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //DO YOUR CODE
        }
    });
</code>```

Body:
I have buttons what I made with "LinearLayout" but when I want to made them clickable and so that it goes to another activity. I understand, that I need to change from "LinearLayout" to "ImageButton" because I want to make a beautiful app [what I want it looks like][1], but when I launch it, it stops working. What can I do to repair that? Thank you in advance for your help and sorry for my bad english. :(

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/header_background"
        android:padding="20dp"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textHello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@string/hello"
            android:textColor="@color/colorWhite"
            android:textSize="25sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="TextContrastCheck" />

        <TextView
            android:id="@+id/textUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@string/vassili"
            android:textColor="@color/colorWhite"
            android:textSize="25sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/textHello"
            tools:ignore="TextContrastCheck" />

        <ImageView
            android:id="@+id/imageMenu"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_menu"
            app:layout_constraintBottom_toBottomOf="@id/textHello"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/textHello"
            app:tint="@color/colorWhite"
            tools:ignore="ImageContrastCheck" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layoutHeader"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="-96dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/layoutGoal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toStartOf="@id/layoutTraining"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/notepad" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/goals"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutTraining"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/layoutGoal"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/dumbbell" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Trainings"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutWater"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toStartOf="@id/layoutTraining"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layoutGoal"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/bottle" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Water"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutStepCounter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/layoutGoal"
                app:layout_constraintTop_toBottomOf="@id/layoutTraining"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/walk" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/step_counter"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

  [1]: https://i.stack.imgur.com/WdW6v.png

Body:
For this type of layout you need to make your LinearLayout clickable
For example:

LinearLayout linearLayoutGoals;

then in you onCreate do findViewById and set its clickListener

linearLayoutGoals = findViewById(R.id.layoutGoal);
linearLayoutGoals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //DO YOUR CODE
        }
    });


问:
Iranian Grade Average Regex
I need a regex to handle the Iranian Grade average, regex should cover all these rules

Range of number should be between 0 to 20.

Numbers could be decimal except 20.

Integer part and decimal part should include one or two numbers.

If you use '.' you should have at least one decimal number.

examples:

20      true

19.99   true

14.5    true

12      true

4       true

0.22    true

00.02   true

10      true

14.23   true

09.23   true

21      false

30      false

123     false

12.222  false

09.     false

4.      false

.2      false

099     false

1.123   false

答:
If it's to match a string with an Iranian Grade Average, then this regex pattern will match only the true ones.

```<code>^(?:[01]?[0-9]|20)(?:[.][0-9]{1,2})?$
</code>```

^ : start of a line or string

(?:[01]?[0-9]|20) : number from 00 or 0 to 19, or 20

(?:[.][0-9]{1,2})? : optional 1 to 2 decimals

$ : end of line or string

Title: Iranian Grade Average Regex

Body:
If it's to match a string with an Iranian Grade Average, then this regex pattern will match only the true ones.

^(?:[01]?[0-9]|20)(?:[.][0-9]{1,2})?$

^ : start of a line or string

(?:[01]?[0-9]|20) : number from 00 or 0 to 19, or 20

(?:[.][0-9]{1,2})? : optional 1 to 2 decimals

$ : end of line or string


问:
split background into 3 divs flippable
I'm trying to apply a background into my html/css website but I also want to split this background into 3 divs who can flip when the cursor is hover.

So I guess the best to do could be to make 3 divs side by side (the total size of my screen) then to apply 3 backgrounds to these 3 divs but each of them are a different part of my background image.

Is anyone here can show me how to do that with a snippet please ?

答:
I have solved this question using an SVG file while you are using to use the same image but a separate part of it. Please let me know if you are looking for something like this, also, upvote my answer,
I have some difficulties while uploading this code sample, still not what I wanted, but I'll let you what I have created for ya, I used the same image side by side, both of them complete the one image. try to re-create this sample below and let me know.

I forget to tell you, in the viewbox you can easily change that.

.flip-card {
                background-color: transparent;
                width: 400px;
                height: 400px;
                perspective: 1000px;
            }

            .flip-card-inner {
                position: relative;
                width: 100%;
                height: 100%;
                text-align: center;
                transition: transform 0.6s;
                transform-style: preserve-3d;
                box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
            }

            .flip-card:hover .flip-card-inner {
                transform: rotateY(180deg);
            }

            .flip-card-front, .flip-card-back {
                position: absolute;
                width: 100%;
                height: 100%;
                -webkit-backface-visibility: hidden;
                backface-visibility: hidden;
            }

            .flip-card-front {
                background-color: #bbb;
                color: rgba(0, 0, 0, 0.62);
            }

            .flip-card-back {
                background-color: #2980b9;
                color: white;
                transform: rotateY(180deg);
            }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container mt-5">

        <h2>Let me know if you still need help Peter</h2>

        <div class="row row-cols-3">
            <div class="flip-card" id="card1">
                <div class="flip-card-inner">
                    <div class="flip-card-front">

                        <svg version="1.0" xmlns="http://www.w3.org/2000/svg"
                             width="300.000000pt" height="300.000000pt" viewBox="-150 0 400.000000 400.000000" preserveAspectRatio="xMidYMid meet">
                            <g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
                                <path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
                                -2000z m620 -100 l0 -320 -25 0 -25 0 0 320 0 320 25 0 25 0 0 -320z m2700 0
                                l0 -320 -25 0 -25 0 0 295 0 296 -84 -71 c-47 -38 -87 -70 -90 -70 -3 0 -6 10
                                -5 23 0 29 170 167 205 167 l24 0 0 -320z m-2292 123 c13 -8 28 -25 34 -39
                                l11 -24 32 34 c60 65 162 67 209 3 19 -26 21 -44 24 -223 3 -188 3 -194 -17
                                -194 -19 0 -20 8 -23 191 l-3 191 -28 24 c-35 30 -85 31 -121 3 -50 -40 -56
                                -64 -56 -244 0 -155 -1 -165 -19 -165 -17 0 -19 12 -23 178 -5 217 -18 251
                                -98 251 -41 1 -70 -20 -99 -69 -19 -32 -21 -51 -21 -197 0 -156 -1 -163 -20
                                -163 -19 0 -20 7 -20 230 0 223 1 230 20 230 16 0 20 -7 20 -37 l0 -38 23 28
                                c41 51 119 65 175 30z m660 16 c30 -6 59 -20 73 -35 23 -25 24 -31 27 -225 3
                                -175 1 -199 -12 -199 -12 0 -16 10 -16 40 0 22 -2 40 -4 40 -2 0 -20 -16 -40
                                -35 -81 -76 -209 -68 -254 16 -8 14 -11 46 -9 78 3 47 9 59 40 87 40 37 79 49
                                185 59 l72 7 0 34 c0 18 -5 44 -11 58 -24 51 -126 60 -246 22 -19 -6 -23 -4
                                -23 13 0 24 42 38 157 50 7 0 34 -4 61 -10z m422 0 c28 -6 64 -22 80 -36 l30
                                -25 0 31 c0 25 4 31 21 31 20 0 21 -4 17 -104 -3 -117 -15 -144 -79 -181 -38
                                -22 -159 -27 -195 -7 -25 14 -44 -1 -44 -34 0 -36 26 -47 130 -55 153 -12 200
                                -45 200 -140 0 -35 -6 -48 -38 -79 -48 -49 -103 -64 -202 -57 -96 7 -140 22
                                -140 49 0 15 4 18 18 13 119 -51 260 -39 306 27 19 26 21 71 5 100 -15 28 -69
                                48 -128 48 -111 0 -191 34 -191 82 0 11 7 32 15 48 14 27 14 32 -5 62 -37 62
                                -18 157 41 201 22 16 63 30 101 36 4 1 30 -4 58 -10z m507 -10 c52 -19 82 -76
                                99 -186 l6 -43 -172 0 -173 0 7 -42 c9 -57 33 -104 62 -123 47 -30 145 -35
                                223 -10 15 5 21 2 21 -10 0 -36 -129 -55 -213 -31 -63 18 -106 61 -126 125
                                -14 48 -14 154 0 204 29 105 152 158 266 116z"/>
                                <path d="M1571 1816 c-120 -43 -103 -206 22 -206 87 0 157 75 157 166 l0 54
                                -72 -1 c-40 0 -88 -6 -107 -13z"/>
                                <path d="M1972 1976 c-34 -32 -37 -38 -37 -91 0 -51 3 -60 31 -84 78 -70 241
                                    -31 245 58 5 105 -40 151 -146 151 -49 0 -59 -4 -93 -34z"/>
                                <path d="M2453 1990 c-37 -22 -60 -61 -69 -119 l-7 -41 147 0 146 0 0 35 c0
                                    48 -22 97 -55 123 -37 29 -117 30 -162 2z"/>
                            </g>
                        </svg>

                    </div>
                    <div class="flip-card-back">
                        <h1>Div One</h1>
                        <span>Place some text here peter....</span>
                    </div>
                </div>
            </div>

            <div class="flip-card" id="card2">
                <div class="flip-card-inner">
                    <div class="flip-card-front">

                        <svg version="1.0" xmlns="http://www.w3.org/2000/svg"
                             width="300.000000pt" height="300.000000pt" viewBox="250 0 400.000000 400.000000" preserveAspectRatio="xMidYMid meet">
                            <g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
                                <path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
                                -2000z m620 -100 l0 -320 -25 0 -25 0 0 320 0 320 25 0 25 0 0 -320z m2700 0
                                l0 -320 -25 0 -25 0 0 295 0 296 -84 -71 c-47 -38 -87 -70 -90 -70 -3 0 -6 10
                                -5 23 0 29 170 167 205 167 l24 0 0 -320z m-2292 123 c13 -8 28 -25 34 -39
                                l11 -24 32 34 c60 65 162 67 209 3 19 -26 21 -44 24 -223 3 -188 3 -194 -17
                                -194 -19 0 -20 8 -23 191 l-3 191 -28 24 c-35 30 -85 31 -121 3 -50 -40 -56
                                -64 -56 -244 0 -155 -1 -165 -19 -165 -17 0 -19 12 -23 178 -5 217 -18 251
                                -98 251 -41 1 -70 -20 -99 -69 -19 -32 -21 -51 -21 -197 0 -156 -1 -163 -20
                                -163 -19 0 -20 7 -20 230 0 223 1 230 20 230 16 0 20 -7 20 -37 l0 -38 23 28
                                c41 51 119 65 175 30z m660 16 c30 -6 59 -20 73 -35 23 -25 24 -31 27 -225 3
                                -175 1 -199 -12 -199 -12 0 -16 10 -16 40 0 22 -2 40 -4 40 -2 0 -20 -16 -40
                                -35 -81 -76 -209 -68 -254 16 -8 14 -11 46 -9 78 3 47 9 59 40 87 40 37 79 49
                                185 59 l72 7 0 34 c0 18 -5 44 -11 58 -24 51 -126 60 -246 22 -19 -6 -23 -4
                                -23 13 0 24 42 38 157 50 7 0 34 -4 61 -10z m422 0 c28 -6 64 -22 80 -36 l30
                                -25 0 31 c0 25 4 31 21 31 20 0 21 -4 17 -104 -3 -117 -15 -144 -79 -181 -38
                                -22 -159 -27 -195 -7 -25 14 -44 -1 -44 -34 0 -36 26 -47 130 -55 153 -12 200
                                -45 200 -140 0 -35 -6 -48 -38 -79 -48 -49 -103 -64 -202 -57 -96 7 -140 22
                                -140 49 0 15 4 18 18 13 119 -51 260 -39 306 27 19 26 21 71 5 100 -15 28 -69
                                48 -128 48 -111 0 -191 34 -191 82 0 11 7 32 15 48 14 27 14 32 -5 62 -37 62
                                -18 157 41 201 22 16 63 30 101 36 4 1 30 -4 58 -10z m507 -10 c52 -19 82 -76
                                99 -186 l6 -43 -172 0 -173 0 7 -42 c9 -57 33 -104 62 -123 47 -30 145 -35
                                223 -10 15 5 21 2 21 -10 0 -36 -129 -55 -213 -31 -63 18 -106 61 -126 125
                                -14 48 -14 154 0 204 29 105 152 158 266 116z"/>
                                <path d="M1571 1816 c-120 -43 -103 -206 22 -206 87 0 157 75 157 166 l0 54
                                -72 -1 c-40 0 -88 -6 -107 -13z"/>
                                <path d="M1972 1976 c-34 -32 -37 -38 -37 -91 0 -51 3 -60 31 -84 78 -70 241
                                    -31 245 58 5 105 -40 151 -146 151 -49 0 -59 -4 -93 -34z"/>
                                <path d="M2453 1990 c-37 -22 -60 -61 -69 -119 l-7 -41 147 0 146 0 0 35 c0
                                    48 -22 97 -55 123 -37 29 -117 30 -162 2z"/>
                            </g>
                        </svg>

                    </div>
                    <div class="flip-card-back">
                        <h1>Div Two</h1>
                        <span>Place some text here peter....</span>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script></code>```

I have used a dummy image here at dummyimage.com more help about SVG at www.w3.org/TR/SVG/pservers.html#Patterns and a useful website to view paths at mapsvg.com

Title: split background into 3 divs flippable

Body:
I have solved this question using an SVG file while you are using to use the same image but a separate part of it. Please let me know if you are looking for something like this, also, upvote my answer,
I have some difficulties while uploading this code sample, still not what I wanted, but I'll let you what I have created for ya, I used the same image side by side, both of them complete the one image. try to re-create this sample below and let me know.

I forget to tell you, in the viewbox you can easily change that.

.flip-card {
                background-color: transparent;
                width: 400px;
                height: 400px;
                perspective: 1000px;
            }

            .flip-card-inner {
                position: relative;
                width: 100%;
                height: 100%;
                text-align: center;
                transition: transform 0.6s;
                transform-style: preserve-3d;
                box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
            }

            .flip-card:hover .flip-card-inner {
                transform: rotateY(180deg);
            }

            .flip-card-front, .flip-card-back {
                position: absolute;
                width: 100%;
                height: 100%;
                -webkit-backface-visibility: hidden;
                backface-visibility: hidden;
            }

            .flip-card-front {
                background-color: #bbb;
                color: rgba(0, 0, 0, 0.62);
            }

            .flip-card-back {
                background-color: #2980b9;
                color: white;
                transform: rotateY(180deg);
            }

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container mt-5">

        <h2>Let me know if you still need help Peter</h2>

        <div class="row row-cols-3">
            <div class="flip-card" id="card1">
                <div class="flip-card-inner">
                    <div class="flip-card-front">

                        <svg version="1.0" xmlns="http://www.w3.org/2000/svg"
                             width="300.000000pt" height="300.000000pt" viewBox="-150 0 400.000000 400.000000" preserveAspectRatio="xMidYMid meet">
                            <g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
                                <path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
                                -2000z m620 -100 l0 -320 -25 0 -25 0 0 320 0 320 25 0 25 0 0 -320z m2700 0
                                l0 -320 -25 0 -25 0 0 295 0 296 -84 -71 c-47 -38 -87 -70 -90 -70 -3 0 -6 10
                                -5 23 0 29 170 167 205 167 l24 0 0 -320z m-2292 123 c13 -8 28 -25 34 -39
                                l11 -24 32 34 c60 65 162 67 209 3 19 -26 21 -44 24 -223 3 -188 3 -194 -17
                                -194 -19 0 -20 8 -23 191 l-3 191 -28 24 c-35 30 -85 31 -121 3 -50 -40 -56
                                -64 -56 -244 0 -155 -1 -165 -19 -165 -17 0 -19 12 -23 178 -5 217 -18 251
                                -98 251 -41 1 -70 -20 -99 -69 -19 -32 -21 -51 -21 -197 0 -156 -1 -163 -20
                                -163 -19 0 -20 7 -20 230 0 223 1 230 20 230 16 0 20 -7 20 -37 l0 -38 23 28
                                c41 51 119 65 175 30z m660 16 c30 -6 59 -20 73 -35 23 -25 24 -31 27 -225 3
                                -175 1 -199 -12 -199 -12 0 -16 10 -16 40 0 22 -2 40 -4 40 -2 0 -20 -16 -40
                                -35 -81 -76 -209 -68 -254 16 -8 14 -11 46 -9 78 3 47 9 59 40 87 40 37 79 49
                                185 59 l72 7 0 34 c0 18 -5 44 -11 58 -24 51 -126 60 -246 22 -19 -6 -23 -4
                                -23 13 0 24 42 38 157 50 7 0 34 -4 61 -10z m422 0 c28 -6 64 -22 80 -36 l30
                                -25 0 31 c0 25 4 31 21 31 20 0 21 -4 17 -104 -3 -117 -15 -144 -79 -181 -38
                                -22 -159 -27 -195 -7 -25 14 -44 -1 -44 -34 0 -36 26 -47 130 -55 153 -12 200
                                -45 200 -140 0 -35 -6 -48 -38 -79 -48 -49 -103 -64 -202 -57 -96 7 -140 22
                                -140 49 0 15 4 18 18 13 119 -51 260 -39 306 27 19 26 21 71 5 100 -15 28 -69
                                48 -128 48 -111 0 -191 34 -191 82 0 11 7 32 15 48 14 27 14 32 -5 62 -37 62
                                -18 157 41 201 22 16 63 30 101 36 4 1 30 -4 58 -10z m507 -10 c52 -19 82 -76
                                99 -186 l6 -43 -172 0 -173 0 7 -42 c9 -57 33 -104 62 -123 47 -30 145 -35
                                223 -10 15 5 21 2 21 -10 0 -36 -129 -55 -213 -31 -63 18 -106 61 -126 125
                                -14 48 -14 154 0 204 29 105 152 158 266 116z"/>
                                <path d="M1571 1816 c-120 -43 -103 -206 22 -206 87 0 157 75 157 166 l0 54
                                -72 -1 c-40 0 -88 -6 -107 -13z"/>
                                <path d="M1972 1976 c-34 -32 -37 -38 -37 -91 0 -51 3 -60 31 -84 78 -70 241
                                    -31 245 58 5 105 -40 151 -146 151 -49 0 -59 -4 -93 -34z"/>
                                <path d="M2453 1990 c-37 -22 -60 -61 -69 -119 l-7 -41 147 0 146 0 0 35 c0
                                    48 -22 97 -55 123 -37 29 -117 30 -162 2z"/>
                            </g>
                        </svg>

                    </div>
                    <div class="flip-card-back">
                        <h1>Div One</h1>
                        <span>Place some text here peter....</span>
                    </div>
                </div>
            </div>

            <div class="flip-card" id="card2">
                <div class="flip-card-inner">
                    <div class="flip-card-front">

                        <svg version="1.0" xmlns="http://www.w3.org/2000/svg"
                             width="300.000000pt" height="300.000000pt" viewBox="250 0 400.000000 400.000000" preserveAspectRatio="xMidYMid meet">
                            <g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
                                <path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
                                -2000z m620 -100 l0 -320 -25 0 -25 0 0 320 0 320 25 0 25 0 0 -320z m2700 0
                                l0 -320 -25 0 -25 0 0 295 0 296 -84 -71 c-47 -38 -87 -70 -90 -70 -3 0 -6 10
                                -5 23 0 29 170 167 205 167 l24 0 0 -320z m-2292 123 c13 -8 28 -25 34 -39
                                l11 -24 32 34 c60 65 162 67 209 3 19 -26 21 -44 24 -223 3 -188 3 -194 -17
                                -194 -19 0 -20 8 -23 191 l-3 191 -28 24 c-35 30 -85 31 -121 3 -50 -40 -56
                                -64 -56 -244 0 -155 -1 -165 -19 -165 -17 0 -19 12 -23 178 -5 217 -18 251
                                -98 251 -41 1 -70 -20 -99 -69 -19 -32 -21 -51 -21 -197 0 -156 -1 -163 -20
                                -163 -19 0 -20 7 -20 230 0 223 1 230 20 230 16 0 20 -7 20 -37 l0 -38 23 28
                                c41 51 119 65 175 30z m660 16 c30 -6 59 -20 73 -35 23 -25 24 -31 27 -225 3
                                -175 1 -199 -12 -199 -12 0 -16 10 -16 40 0 22 -2 40 -4 40 -2 0 -20 -16 -40
                                -35 -81 -76 -209 -68 -254 16 -8 14 -11 46 -9 78 3 47 9 59 40 87 40 37 79 49
                                185 59 l72 7 0 34 c0 18 -5 44 -11 58 -24 51 -126 60 -246 22 -19 -6 -23 -4
                                -23 13 0 24 42 38 157 50 7 0 34 -4 61 -10z m422 0 c28 -6 64 -22 80 -36 l30
                                -25 0 31 c0 25 4 31 21 31 20 0 21 -4 17 -104 -3 -117 -15 -144 -79 -181 -38
                                -22 -159 -27 -195 -7 -25 14 -44 -1 -44 -34 0 -36 26 -47 130 -55 153 -12 200
                                -45 200 -140 0 -35 -6 -48 -38 -79 -48 -49 -103 -64 -202 -57 -96 7 -140 22
                                -140 49 0 15 4 18 18 13 119 -51 260 -39 306 27 19 26 21 71 5 100 -15 28 -69
                                48 -128 48 -111 0 -191 34 -191 82 0 11 7 32 15 48 14 27 14 32 -5 62 -37 62
                                -18 157 41 201 22 16 63 30 101 36 4 1 30 -4 58 -10z m507 -10 c52 -19 82 -76
                                99 -186 l6 -43 -172 0 -173 0 7 -42 c9 -57 33 -104 62 -123 47 -30 145 -35
                                223 -10 15 5 21 2 21 -10 0 -36 -129 -55 -213 -31 -63 18 -106 61 -126 125
                                -14 48 -14 154 0 204 29 105 152 158 266 116z"/>
                                <path d="M1571 1816 c-120 -43 -103 -206 22 -206 87 0 157 75 157 166 l0 54
                                -72 -1 c-40 0 -88 -6 -107 -13z"/>
                                <path d="M1972 1976 c-34 -32 -37 -38 -37 -91 0 -51 3 -60 31 -84 78 -70 241
                                    -31 245 58 5 105 -40 151 -146 151 -49 0 -59 -4 -93 -34z"/>
                                <path d="M2453 1990 c-37 -22 -60 -61 -69 -119 l-7 -41 147 0 146 0 0 35 c0
                                    48 -22 97 -55 123 -37 29 -117 30 -162 2z"/>
                            </g>
                        </svg>

                    </div>
                    <div class="flip-card-back">
                        <h1>Div Two</h1>
                        <span>Place some text here peter....</span>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

I have used a dummy image here at dummyimage.com more help about SVG at www.w3.org/TR/SVG/pservers.html#Patterns and a useful website to view paths at mapsvg.com


问:
How to use router.navigateByUrl and router.navigate in Angular
https://angular.io/api/router/RouterLink gives a good overview of how to create links that will take the user to a different route in Angular4, however I can't find how to do the same thing programmatically rather needing the user to click a link</p>

答:
I faced the same issue. My solution:</p>
```<code>...
<p @click=parseHTML v-html=data.html></p>
...
methods: {
  parseHTML(event) {
    if (event.target.href) {
      event.preventDefault();
      if (event.target.href.includes(location.hostname)) {
        this.$router.push(event.target.href)
      }
    }
  }
}
</code>```

Body:
I faced the same issue. My solution:

...
<p @click=parseHTML v-html=data.html></p>
...
methods: {
  parseHTML(event) {
    if (event.target.href) {
      event.preventDefault();
      if (event.target.href.includes(location.hostname)) {
        this.$router.push(event.target.href)
      }
    }
  }
}


问:
Compare Matrix Multiplication Execution Time in Different Sizes in C
I need to calculate and compare execution time of multiplication of 2 matrices in 3 different sizes (100 * 100 , 1000 * 1000 and 10000 * 10000) in C programming language. I wrote the following simple code to do that for 1000 * 1000 and I got the execution time

```<code>#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r1 = 1000, c1 = 1000, r2 = 1000, c2 = 1000, i, j, k;

    // Dynamic allocation.
    double(*a)[r1][c1] = malloc(sizeof *a);
    double(*b)[r2][c2] = malloc(sizeof *b);
    double(*result)[r1][c2] = malloc(sizeof *result);

    // Storing elements of first matrix.
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c1; ++j)
        {
            (*a)[i][j] = rand() / RAND_MAX;
        }
    }

    // Storing elements of second matrix.
    for (i = 0; i < r2; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*b)[i][j] = rand() / RAND_MAX;
        }
    }

    // Initializing all elements of result matrix to 0
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*result)[i][j] = 0;
        }
    }

    clock_t begin1 = clock();
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (i = 0; i < r1; ++i)
        for (j = 0; j < c2; ++j)
            for (k = 0; k < c1; ++k)
            {
                (*result)[i][j] += (*a)[i][k] * (*b)[k][j];
            }

    clock_t end1 = clock();
    double time_taken = (double)(end1 - begin1) / CLOCKS_PER_SEC;
    printf("\n function took %f seconds to execute \n", time_taken);
    return 0;
}
</code>```

And now I want to repeat this part for two other sizes and get the result like this at the end of my program with one run:

```<code>the execution time for 100 * 100 is 1 second
the execution time for 1000 * 1000 is 2 seconds
the execution time for 10000 * 10000 is 3 seconds
</code>```

What is the best solution for that? When I repeat this part for 10000 * 10000 after 1000 * 1000 I got the segmentation fault error.

```<code>#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r1 = 1000, c1 = 1000, r2 = 1000, c2 = 1000, i, j, k;

    // Dynamic allocation.
    double(*a)[r1][c1] = malloc(sizeof *a);
    double(*b)[r2][c2] = malloc(sizeof *b);
    double(*result)[r1][c2] = malloc(sizeof *result);

    // Storing elements of first matrix.
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c1; ++j)
        {
            (*a)[i][j] = rand() / RAND_MAX;
        }
    }

    // Storing elements of second matrix.
    for (i = 0; i < r2; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*b)[i][j] = rand() / RAND_MAX;
        }
    }

    // Initializing all elements of result matrix to 0
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*result)[i][j] = 0;
        }
    }

    clock_t begin1 = clock();
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (i = 0; i < r1; ++i)
        for (j = 0; j < c2; ++j)
            for (k = 0; k < c1; ++k)
            {
                (*result)[i][j] += (*a)[i][k] * (*b)[k][j];
            }

    clock_t end1 = clock();
    double time_taken = (double)(end1 - begin1) / CLOCKS_PER_SEC;
    printf("\n \nfunction took %f seconds to execute \n",
           time_taken);
    free(a);
    free(b);
    free(result);

    r1 = 10000, c1 = 10000, r2 = 10000, c2 = 10000;
printf("\n run second one for %d \n",r1);
    // Storing elements of first matrix.
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c1; ++j)
        {
            (*a)[i][j] = rand() / RAND_MAX;
        }
    }

    // Storing elements of second matrix.
    for (i = 0; i < r2; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*b)[i][j] = rand() / RAND_MAX;
        }
    }

    // Initializing all elements of result matrix to 0
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*result)[i][j] = 0;
        }
    }

    begin1 = clock();
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (i = 0; i < r1; ++i)
        for (j = 0; j < c2; ++j)
            for (k = 0; k < c1; ++k)
            {
                (*result)[i][j] += (*a)[i][k] * (*b)[k][j];
            }

    end1 = clock();
    time_taken = (double)(end1 - begin1) / CLOCKS_PER_SEC;
    printf("\n second function took %f seconds to execute \n",
           time_taken);
    free(a);
    free(b);
    free(result);

    return 0;
}
</code>```

答:
And now I want to repeat this part for two other sizes and get the result like this at the end of my program with one run: the execution time for 100 * 100 is 1 second the execution time for 1000 * 1000 is 2 seconds the execution time for 10000 * 10000 is 3 seconds

I simply cannot believe that you have multiplied two 10,000 x 10,000 matrices in 3 seconds.  What computer are you running that experiment in?  Not, with that algorithm and using only one core.  Probably you are optimizing your compilation (with default flag -O2 and the whole algorithm has been evicted from the compiler output, as you don't use the matrix after the calculation, so it is innecessary to lose time in a loop for nothing)  first, compile your matrix, or print after the computation one element of the result matrix, so the compiler cannot evict the calculation.  But don't say your algorithm is multiplying two matrices 10,000 rows and columns in 3 sec.

Allocating a matrix of 10,000x10,000 double quantities, requires a lot of memory.  It's 100,000,000 of double entries, wich gets over 800Mb in a single malloc  It's very possible that your laptop can handle this once.... but don't do many of these allocations, as you will probably will be over the limits of malloc(3) (or your system).

More when you need at least two of these allocations, or even more, as you said you want to repeat the calculations.

Have you trying to scale yor problem to 100,000 by 100,000 matrices?

When you repeat, it's not warranted that there's no fragmentation in the heap maintained by malloc(), so as you are requesting one gigabyte of continuous memory per matrix, it is probable that malloc(3) runs out of memory in the last mallocs.  I'd suggest you to do the three tests in different programs (separate), and don't start any other program (like the browser or your favourite desktop environment while running a matrix multiplication involving 200,000,000 numbers.

Anyway, your program probably start doing swapping if you don't fine control your execution environment, just trashing all the efficiency measurements you are trying to do.

Another thing is that you can probably could incurr in a process limit (if your administrator has established a maximum core memory limmit for your process) and you don't check the result from malloc(3) for proper allocation.

Note:

I have not been able to multiply the two 10,000 by 10,000 matrices in my machine (Pentium Core duo with 6Gb RAM) because the program starts swaping and becomes incredible slow.  BTW, be careful, and don't compile your program with optimization (even -O1 makes the matrix multiplication to be eliminated completely from the code, as there's no use of the product, so the complete algorithm is eliminated from the output code by the optimization code in the compiler)  I'll leave it running and update my answer if I get some result.

(edit)

```<code>$ matrix_mult
function took 9364.303443 seconds to execute
$
</code>```

It took 2h 36m 6.303443s to execute in my system.  This is more approximate to the complexity of the problem (as your approximation is barely linear)  you need to compile it without optimization or at least print one element of the resultant matrix, if you want the product being calculated.

Below is the code I'm testing, if you want to test it (I have modified it a bit to make it more readable, to use better timestamps, and to avoid the C initialization, as it can be done on the fly (read the comments in the code):

```<code>#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10000

int main()
{

    // Dynamic allocation.
#define A (*a)
#define B (*b)
#define R (*result)

    double A[N][N] = malloc(sizeof *a);
    double B[N][N] = malloc(sizeof *b);
    double R[N][N] = malloc(sizeof *result);

    printf("Initializing both matrices\n");
    // Storing elements of first (and second) matrices.
    for (int row = 0; row < N; ++row) {
        for (int col = 0; col < N; ++col) {
            A[row][col] = rand() / (double)RAND_MAX; // matrix A
            B[row][col] = rand() / (double)RAND_MAX; // matrix B
        }
    }

    // Storing elements of second matrix. (done above)

    // Initializing all elements of result matrix to 0
    // (not needed, see below)

    printf("Starting multiplication\n");
    struct timespec start_ts; // start timestamp (secs & nsecs).
    int res = clock_gettime(
            CLOCK_PROCESS_CPUTIME_ID,
            &start_ts); // cpu time only.
    if (res < 0) {
        perror("clock_gettime");
        exit(EXIT_FAILURE);
    }
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (int row = 0; row < N; ++row) {
        for (int col = 0; col < N; ++col) {
            double aux = 0.0;
            for (int k = 0; k < N; ++k) {
                aux += A[row][k] * B[k][col];
            }
            // why to involve calculating the address of the affected
            // cell at every inner loop iteration???
            R[row][col] = aux;
        }
    }
    struct timespec end_ts;
    res = clock_gettime(
            CLOCK_PROCESS_CPUTIME_ID,
            &end_ts);
    if (res < 0) {
        perror("clock_gettime");
        exit(EXIT_FAILURE);
    }

    bool carry = start_ts.tv_nsec > end_ts.tv_nsec;
    struct timespec diff_time;
    diff_time.tv_sec  = end_ts.tv_sec  - start_ts.tv_sec;
    diff_time.tv_nsec = end_ts.tv_nsec - start_ts.tv_nsec;
    if (carry) {
        diff_time.tv_sec--;
        diff_time.tv_nsec += 1000000000;
    }
    printf("\n function took %ld.%06ld seconds to execute \n",
            diff_time.tv_sec, diff_time.tv_nsec / 1000);
    return 0;
}
</code>```

2nd edit

I have tested multiplication times on a modified version of your program (but using the same algorithm) with the program below:

```<code>#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 10000

int dim[] = {
    89, 144, 233, 377, 610,
    987, 1597, 2584, 4181, 6765,
    10000
};  /* several numbers taken from fibonacci numbers */
size_t dim_cnt = sizeof dim / sizeof dim[0];

double A[MAX][MAX];
double B[MAX][MAX];
double R[MAX][MAX];

int main()
{

    for (int rep = 0; rep < dim_cnt; rep++) {

        size_t N = dim[rep];

        printf("It %d: Initializing both %zd x %zd matrices\n",
                rep, N, N);
        // Storing elements of first (and second) matrices.
        for (int row = 0; row < N; ++row) {
            for (int col = 0; col < N; ++col) {
                A[row][col] = rand() /(double)RAND_MAX; // matrix A
                B[row][col] = rand() /(double)RAND_MAX; // matrix B
            }
        }

        // Storing elements of second matrix. (done above)

        // Initializing all elements of result matrix to 0
        // (not needed, see below)

        printf("It %d: Starting multiplication\n", rep);
        struct timespec start_ts; // start timestamp (secs & nsecs).
        int res = clock_gettime(
                CLOCK_PROCESS_CPUTIME_ID,
                &start_ts); // cpu time only.
        if (res < 0) {
            perror("clock_gettime");
            exit(EXIT_FAILURE);
        }
        // Multiplying matrices a and b and
        // storing result in result matrix
        for (int row = 0; row < N; ++row) {
            for (int col = 0; col < N; ++col) {
                double aux = 0.0;
                for (int k = 0; k < N; ++k) {
                    aux += A[row][k] * B[k][col];
                }
                // why to involve calculating the address of the affected
                // cell at every inner loop iteration???
                R[row][col] = aux;
            }
        }
        struct timespec end_ts;
        res = clock_gettime(
                CLOCK_PROCESS_CPUTIME_ID,
                &end_ts);
        if (res < 0) {
            perror("clock_gettime");
            exit(EXIT_FAILURE);
        }

        bool carry = start_ts.tv_nsec > end_ts.tv_nsec;
        struct timespec diff_time;
        diff_time.tv_sec  = end_ts.tv_sec  - start_ts.tv_sec;
        diff_time.tv_nsec = end_ts.tv_nsec - start_ts.tv_nsec;
        if (carry) {
            diff_time.tv_sec--;
            diff_time.tv_nsec += 1000000000;
        }
        printf("It %d: R[0][0] = %g\n", rep, R[0][0]);
        printf("%7zd %ld.%06ld\n",
                N,
                diff_time.tv_sec,
                diff_time.tv_nsec / 1000);
    }
    return 0;
}
</code>```

It uses a single, statically allocated memory for the biggest of the matrices, and uses a subset of it to model the lower ones.  The execution times are far more appropiate than the values you shown in your doce, and I print a result matrix cell value to force the optimizer to conserve the matrix calculation.  The resulting execution you get should show values proportional to the ones shown here.

```<code>$ time matrix_mult
It 0: Initializing both 89 x 89 matrices
It 0: Starting multiplication
It 0: R[0][0] = 23.6756
     89 0.005026
It 1: Initializing both 144 x 144 matrices
It 1: Starting multiplication
It 1: R[0][0] = 40.2614
    144 0.019682
It 2: Initializing both 233 x 233 matrices
It 2: Starting multiplication
It 2: R[0][0] = 59.5599
    233 0.095213
It 3: Initializing both 377 x 377 matrices
It 3: Starting multiplication
It 3: R[0][0] = 93.4422
    377 0.392914
It 4: Initializing both 610 x 610 matrices
It 4: Starting multiplication
It 4: R[0][0] = 153.068
    610 1.671904
It 5: Initializing both 987 x 987 matrices
It 5: Starting multiplication
It 5: R[0][0] = 252.981
    987 8.816252
It 6: Initializing both 1597 x 1597 matrices
It 6: Starting multiplication
It 6: R[0][0] = 403.61
   1597 37.807920
It 7: Initializing both 2584 x 2584 matrices
It 7: Starting multiplication
It 7: R[0][0] = 629.521
   2584 157.371367
It 8: Initializing both 4181 x 4181 matrices
It 8: Starting multiplication
It 8: R[0][0] = 1036.47
   4181 667.084346
It 9: Initializing both 6765 x 6765 matrices
It 9: Starting multiplication
It 9: R[0][0] = 1653.59
   6765 2831.117818
It 10: Initializing both 10000 x 10000 matrices
It 10: Starting multiplication
It 10: R[0][0] = 2521.68
  10000 9211.738007

real    216m46,129s
user    215m16,041s
sys     0m4,899s
$ _
</code>```

In my system, the program shows the following size

```<code>$ size matrix_mult
  text   data          bss          dec          hex   filename
  2882    528   2400000016   2400003426   0x8f0d2562   matrix_mult
$ _
</code>```

with a 2.4Gb large bss segment, as corresponding to three variables of around 800Mb each.

One last point: Using VLAs and making your program to dynamically allocate things that will be used during all the program life will not help you to make it faster or slower.  It's the algorithm what makes programs faster or slower.  Bu I fear you have not calculated a 10,000 by 10,000 matrix product in 3s.

Body:
I need to calculate and compare execution time of multiplication of 2 matrices in 3 different sizes (100 * 100 , 1000 * 1000 and 10000 * 10000) in C programming language. I wrote the following simple code to do that for 1000 * 1000 and I got the execution time

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r1 = 1000, c1 = 1000, r2 = 1000, c2 = 1000, i, j, k;

    // Dynamic allocation.
    double(*a)[r1][c1] = malloc(sizeof *a);
    double(*b)[r2][c2] = malloc(sizeof *b);
    double(*result)[r1][c2] = malloc(sizeof *result);

    // Storing elements of first matrix.
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c1; ++j)
        {
            (*a)[i][j] = rand() / RAND_MAX;
        }
    }

    // Storing elements of second matrix.
    for (i = 0; i < r2; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*b)[i][j] = rand() / RAND_MAX;
        }
    }

    // Initializing all elements of result matrix to 0
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*result)[i][j] = 0;
        }
    }

    clock_t begin1 = clock();
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (i = 0; i < r1; ++i)
        for (j = 0; j < c2; ++j)
            for (k = 0; k < c1; ++k)
            {
                (*result)[i][j] += (*a)[i][k] * (*b)[k][j];
            }

    clock_t end1 = clock();
    double time_taken = (double)(end1 - begin1) / CLOCKS_PER_SEC;
    printf("\n function took %f seconds to execute \n", time_taken);
    return 0;
}

And now I want to repeat this part for two other sizes and get the result like this at the end of my program with one run:

the execution time for 100 * 100 is 1 second
the execution time for 1000 * 1000 is 2 seconds
the execution time for 10000 * 10000 is 3 seconds

What is the best solution for that? When I repeat this part for 10000 * 10000 after 1000 * 1000 I got the segmentation fault error.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r1 = 1000, c1 = 1000, r2 = 1000, c2 = 1000, i, j, k;

    // Dynamic allocation.
    double(*a)[r1][c1] = malloc(sizeof *a);
    double(*b)[r2][c2] = malloc(sizeof *b);
    double(*result)[r1][c2] = malloc(sizeof *result);

    // Storing elements of first matrix.
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c1; ++j)
        {
            (*a)[i][j] = rand() / RAND_MAX;
        }
    }

    // Storing elements of second matrix.
    for (i = 0; i < r2; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*b)[i][j] = rand() / RAND_MAX;
        }
    }

    // Initializing all elements of result matrix to 0
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*result)[i][j] = 0;
        }
    }

    clock_t begin1 = clock();
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (i = 0; i < r1; ++i)
        for (j = 0; j < c2; ++j)
            for (k = 0; k < c1; ++k)
            {
                (*result)[i][j] += (*a)[i][k] * (*b)[k][j];
            }

    clock_t end1 = clock();
    double time_taken = (double)(end1 - begin1) / CLOCKS_PER_SEC;
    printf("\n \nfunction took %f seconds to execute \n",
           time_taken);
    free(a);
    free(b);
    free(result);

    r1 = 10000, c1 = 10000, r2 = 10000, c2 = 10000;
printf("\n run second one for %d \n",r1);
    // Storing elements of first matrix.
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c1; ++j)
        {
            (*a)[i][j] = rand() / RAND_MAX;
        }
    }

    // Storing elements of second matrix.
    for (i = 0; i < r2; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*b)[i][j] = rand() / RAND_MAX;
        }
    }

    // Initializing all elements of result matrix to 0
    for (i = 0; i < r1; ++i)
    {
        for (j = 0; j < c2; ++j)
        {
            (*result)[i][j] = 0;
        }
    }

    begin1 = clock();
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (i = 0; i < r1; ++i)
        for (j = 0; j < c2; ++j)
            for (k = 0; k < c1; ++k)
            {
                (*result)[i][j] += (*a)[i][k] * (*b)[k][j];
            }

    end1 = clock();
    time_taken = (double)(end1 - begin1) / CLOCKS_PER_SEC;
    printf("\n second function took %f seconds to execute \n",
           time_taken);
    free(a);
    free(b);
    free(result);

    return 0;
}

Body:
And now I want to repeat this part for two other sizes and get the result like this at the end of my program with one run: the execution time for 100 * 100 is 1 second the execution time for 1000 * 1000 is 2 seconds the execution time for 10000 * 10000 is 3 seconds

I simply cannot believe that you have multiplied two 10,000 x 10,000 matrices in 3 seconds.  What computer are you running that experiment in?  Not, with that algorithm and using only one core.  Probably you are optimizing your compilation (with default flag -O2 and the whole algorithm has been evicted from the compiler output, as you don't use the matrix after the calculation, so it is innecessary to lose time in a loop for nothing)  first, compile your matrix, or print after the computation one element of the result matrix, so the compiler cannot evict the calculation.  But don't say your algorithm is multiplying two matrices 10,000 rows and columns in 3 sec.

Allocating a matrix of 10,000x10,000 double quantities, requires a lot of memory.  It's 100,000,000 of double entries, wich gets over 800Mb in a single malloc  It's very possible that your laptop can handle this once.... but don't do many of these allocations, as you will probably will be over the limits of malloc(3) (or your system).

More when you need at least two of these allocations, or even more, as you said you want to repeat the calculations.

Have you trying to scale yor problem to 100,000 by 100,000 matrices?

When you repeat, it's not warranted that there's no fragmentation in the heap maintained by malloc(), so as you are requesting one gigabyte of continuous memory per matrix, it is probable that malloc(3) runs out of memory in the last mallocs.  I'd suggest you to do the three tests in different programs (separate), and don't start any other program (like the browser or your favourite desktop environment while running a matrix multiplication involving 200,000,000 numbers.

Anyway, your program probably start doing swapping if you don't fine control your execution environment, just trashing all the efficiency measurements you are trying to do.

Another thing is that you can probably could incurr in a process limit (if your administrator has established a maximum core memory limmit for your process) and you don't check the result from malloc(3) for proper allocation.

Note:

I have not been able to multiply the two 10,000 by 10,000 matrices in my machine (Pentium Core duo with 6Gb RAM) because the program starts swaping and becomes incredible slow.  BTW, be careful, and don't compile your program with optimization (even -O1 makes the matrix multiplication to be eliminated completely from the code, as there's no use of the product, so the complete algorithm is eliminated from the output code by the optimization code in the compiler)  I'll leave it running and update my answer if I get some result.

(edit)

$ matrix_mult
function took 9364.303443 seconds to execute
$

It took 2h 36m 6.303443s to execute in my system.  This is more approximate to the complexity of the problem (as your approximation is barely linear)  you need to compile it without optimization or at least print one element of the resultant matrix, if you want the product being calculated.

Below is the code I'm testing, if you want to test it (I have modified it a bit to make it more readable, to use better timestamps, and to avoid the C initialization, as it can be done on the fly (read the comments in the code):

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10000

int main()
{

    // Dynamic allocation.
#define A (*a)
#define B (*b)
#define R (*result)

    double A[N][N] = malloc(sizeof *a);
    double B[N][N] = malloc(sizeof *b);
    double R[N][N] = malloc(sizeof *result);

    printf("Initializing both matrices\n");
    // Storing elements of first (and second) matrices.
    for (int row = 0; row < N; ++row) {
        for (int col = 0; col < N; ++col) {
            A[row][col] = rand() / (double)RAND_MAX; // matrix A
            B[row][col] = rand() / (double)RAND_MAX; // matrix B
        }
    }

    // Storing elements of second matrix. (done above)

    // Initializing all elements of result matrix to 0
    // (not needed, see below)

    printf("Starting multiplication\n");
    struct timespec start_ts; // start timestamp (secs & nsecs).
    int res = clock_gettime(
            CLOCK_PROCESS_CPUTIME_ID,
            &start_ts); // cpu time only.
    if (res < 0) {
        perror("clock_gettime");
        exit(EXIT_FAILURE);
    }
    // Multiplying matrices a and b and
    // storing result in result matrix
    for (int row = 0; row < N; ++row) {
        for (int col = 0; col < N; ++col) {
            double aux = 0.0;
            for (int k = 0; k < N; ++k) {
                aux += A[row][k] * B[k][col];
            }
            // why to involve calculating the address of the affected
            // cell at every inner loop iteration???
            R[row][col] = aux;
        }
    }
    struct timespec end_ts;
    res = clock_gettime(
            CLOCK_PROCESS_CPUTIME_ID,
            &end_ts);
    if (res < 0) {
        perror("clock_gettime");
        exit(EXIT_FAILURE);
    }

    bool carry = start_ts.tv_nsec > end_ts.tv_nsec;
    struct timespec diff_time;
    diff_time.tv_sec  = end_ts.tv_sec  - start_ts.tv_sec;
    diff_time.tv_nsec = end_ts.tv_nsec - start_ts.tv_nsec;
    if (carry) {
        diff_time.tv_sec--;
        diff_time.tv_nsec += 1000000000;
    }
    printf("\n function took %ld.%06ld seconds to execute \n",
            diff_time.tv_sec, diff_time.tv_nsec / 1000);
    return 0;
}

2nd edit

I have tested multiplication times on a modified version of your program (but using the same algorithm) with the program below:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 10000

int dim[] = {
    89, 144, 233, 377, 610,
    987, 1597, 2584, 4181, 6765,
    10000
};  /* several numbers taken from fibonacci numbers */
size_t dim_cnt = sizeof dim / sizeof dim[0];

double A[MAX][MAX];
double B[MAX][MAX];
double R[MAX][MAX];

int main()
{

    for (int rep = 0; rep < dim_cnt; rep++) {

        size_t N = dim[rep];

        printf("It %d: Initializing both %zd x %zd matrices\n",
                rep, N, N);
        // Storing elements of first (and second) matrices.
        for (int row = 0; row < N; ++row) {
            for (int col = 0; col < N; ++col) {
                A[row][col] = rand() /(double)RAND_MAX; // matrix A
                B[row][col] = rand() /(double)RAND_MAX; // matrix B
            }
        }

        // Storing elements of second matrix. (done above)

        // Initializing all elements of result matrix to 0
        // (not needed, see below)

        printf("It %d: Starting multiplication\n", rep);
        struct timespec start_ts; // start timestamp (secs & nsecs).
        int res = clock_gettime(
                CLOCK_PROCESS_CPUTIME_ID,
                &start_ts); // cpu time only.
        if (res < 0) {
            perror("clock_gettime");
            exit(EXIT_FAILURE);
        }
        // Multiplying matrices a and b and
        // storing result in result matrix
        for (int row = 0; row < N; ++row) {
            for (int col = 0; col < N; ++col) {
                double aux = 0.0;
                for (int k = 0; k < N; ++k) {
                    aux += A[row][k] * B[k][col];
                }
                // why to involve calculating the address of the affected
                // cell at every inner loop iteration???
                R[row][col] = aux;
            }
        }
        struct timespec end_ts;
        res = clock_gettime(
                CLOCK_PROCESS_CPUTIME_ID,
                &end_ts);
        if (res < 0) {
            perror("clock_gettime");
            exit(EXIT_FAILURE);
        }

        bool carry = start_ts.tv_nsec > end_ts.tv_nsec;
        struct timespec diff_time;
        diff_time.tv_sec  = end_ts.tv_sec  - start_ts.tv_sec;
        diff_time.tv_nsec = end_ts.tv_nsec - start_ts.tv_nsec;
        if (carry) {
            diff_time.tv_sec--;
            diff_time.tv_nsec += 1000000000;
        }
        printf("It %d: R[0][0] = %g\n", rep, R[0][0]);
        printf("%7zd %ld.%06ld\n",
                N,
                diff_time.tv_sec,
                diff_time.tv_nsec / 1000);
    }
    return 0;
}

It uses a single, statically allocated memory for the biggest of the matrices, and uses a subset of it to model the lower ones.  The execution times are far more appropiate than the values you shown in your doce, and I print a result matrix cell value to force the optimizer to conserve the matrix calculation.  The resulting execution you get should show values proportional to the ones shown here.

$ time matrix_mult
It 0: Initializing both 89 x 89 matrices
It 0: Starting multiplication
It 0: R[0][0] = 23.6756
     89 0.005026
It 1: Initializing both 144 x 144 matrices
It 1: Starting multiplication
It 1: R[0][0] = 40.2614
    144 0.019682
It 2: Initializing both 233 x 233 matrices
It 2: Starting multiplication
It 2: R[0][0] = 59.5599
    233 0.095213
It 3: Initializing both 377 x 377 matrices
It 3: Starting multiplication
It 3: R[0][0] = 93.4422
    377 0.392914
It 4: Initializing both 610 x 610 matrices
It 4: Starting multiplication
It 4: R[0][0] = 153.068
    610 1.671904
It 5: Initializing both 987 x 987 matrices
It 5: Starting multiplication
It 5: R[0][0] = 252.981
    987 8.816252
It 6: Initializing both 1597 x 1597 matrices
It 6: Starting multiplication
It 6: R[0][0] = 403.61
   1597 37.807920
It 7: Initializing both 2584 x 2584 matrices
It 7: Starting multiplication
It 7: R[0][0] = 629.521
   2584 157.371367
It 8: Initializing both 4181 x 4181 matrices
It 8: Starting multiplication
It 8: R[0][0] = 1036.47
   4181 667.084346
It 9: Initializing both 6765 x 6765 matrices
It 9: Starting multiplication
It 9: R[0][0] = 1653.59
   6765 2831.117818
It 10: Initializing both 10000 x 10000 matrices
It 10: Starting multiplication
It 10: R[0][0] = 2521.68
  10000 9211.738007

real    216m46,129s
user    215m16,041s
sys     0m4,899s
$ _

In my system, the program shows the following size

$ size matrix_mult
  text   data          bss          dec          hex   filename
  2882    528   2400000016   2400003426   0x8f0d2562   matrix_mult
$ _

with a 2.4Gb large bss segment, as corresponding to three variables of around 800Mb each.

One last point: Using VLAs and making your program to dynamically allocate things that will be used during all the program life will not help you to make it faster or slower.  It's the algorithm what makes programs faster or slower.  Bu I fear you have not calculated a 10,000 by 10,000 matrix product in 3s.


问:
Issue with a `String` and `thiserror`: `as_dyn_error` exists for reference `&String`, but its trait bounds were not satisfied` in Rust
This is my first attempt to using thiserror in Rust. One of the error types I want to provide is a general string which has a message but no other fields. I've attempted to copy this from the thiserror docs, with the only exception being the use of #[from], so that I can simply return Err(String::from("some string")). The code looks like this:

```<code>use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("{0}")]
    Other(#[from] String)
}
</code>```

Playground

However, this gives:

```<code>   Compiling playground v0.0.1 (/playground)
error[E0599]: the method `as_dyn_error` exists for reference `&String`, but its trait bounds were not satisfied
   --> src/lib.rs:5:13
    |
5   |       #[error("{0}")]
    |               ^^^^^ method cannot be called on `&String` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `String: std::error::Error`
            which is required by `String: AsDynError`
            `&String: std::error::Error`
            which is required by `&String: AsDynError`
            `str: Sized`
            which is required by `str: AsDynError`
            `str: std::error::Error`
            which is required by `str: AsDynError`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error
</code>```

What is the issue here, any how can I resolve it, while keeping the From impl?

答:
The [from] tag is used when your error wraps another error. In this case you do not need it. But you need to actually wrap whatever String with the proper Other constructor:

use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("{0}")]
    Other(String)
}

fn main() -> Result<(), Error> {
    Err(Error::Other("Hey oh let's go!".to_string()))
}
```

Playground

Body:
This is my first attempt to using thiserror in Rust. One of the error types I want to provide is a general string which has a message but no other fields. I've attempted to copy this from the thiserror docs, with the only exception being the use of #[from], so that I can simply return Err(String::from("some string")). The code looks like this:

use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("{0}")]
    Other(#[from] String)
}

Playground

However, this gives:

   Compiling playground v0.0.1 (/playground)
error[E0599]: the method `as_dyn_error` exists for reference `&String`, but its trait bounds were not satisfied
   --> src/lib.rs:5:13
    |
5   |       #[error("{0}")]
    |               ^^^^^ method cannot be called on `&String` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `String: std::error::Error`
            which is required by `String: AsDynError`
            `&String: std::error::Error`
            which is required by `&String: AsDynError`
            `str: Sized`
            which is required by `str: AsDynError`
            `str: std::error::Error`
            which is required by `str: AsDynError`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error

What is the issue here, any how can I resolve it, while keeping the From impl?

Body:
The [from] tag is used when your error wraps another error. In this case you do not need it. But you need to actually wrap whatever String with the proper Other constructor:

use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("{0}")]
    Other(String)
}

fn main() -> Result<(), Error> {
    Err(Error::Other("Hey oh let's go!".to_string()))
}

Playground


问:
tsconfig.json: Build:No inputs were found in config file
I have an ASP.NET core project and I'm getting this error when I try to build it:

```<code>error TS18003: Build:No inputs were found in config file 'Z:/Projects/client/ZV/src/ZV/Scripts/tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '["../wwwroot/app","node_modules/*"]'.
1>         The command exited with code 1.
1>       Done executing task "VsTsc" -- FAILED.
</code>```

This is my tsconfig.json file:

```<code>{
  "compileOnSave": true,
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es5", "dom" ],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../wwwroot/app/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es6"
  },
  "exclude": [
    "../wwwroot/app",
    "node_modules/*"
  ]
}
</code>```

Is this a bug or am I doing something wrong? I did recently upgrade Visual Studio 2015 to update 3. Has anyone encountered this before?

答:
I used that
How to disable TypeScript compilation in .Net Core projects?

Because I use a template and those packages require for only my template.
However I can see every packages' export in their node_module files.
If you are in same situation you can use link above

Body:
I have an ASP.NET core project and I'm getting this error when I try to build it:

error TS18003: Build:No inputs were found in config file 'Z:/Projects/client/ZV/src/ZV/Scripts/tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '["../wwwroot/app","node_modules/*"]'.
1>         The command exited with code 1.
1>       Done executing task "VsTsc" -- FAILED.

This is my tsconfig.json file:

{
  "compileOnSave": true,
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es5", "dom" ],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../wwwroot/app/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es6"
  },
  "exclude": [
    "../wwwroot/app",
    "node_modules/*"
  ]
}

Is this a bug or am I doing something wrong? I did recently upgrade Visual Studio 2015 to update 3. Has anyone encountered this before?


问:
What should i add in path for using mvn commands if i have only maven wrapper from Inteleji?
I have faced such a problem: i have a home project with maven, it was pulled automatically through Inteligi IDE and now i need to change my liquibase checkSum using mvn commands, but it doesn't work cause my machine can't recognize this command.

I tried to add in path the following directories:
C:\Users\myUser\Desktop\tutorial\tutorial.mvn\wrapper
C:\Users\myUser\m2\repository

i tried to use commands mvn, mvnw and it didn't work out

Basically i need to set up maven on my computer and add it to Path variable but i use maven wrapper there should be some way not to setup regular maven. If you have some ideas i'll be grateful

答:
I have found the correct path, it's C:\Users\MyUser.m2\wrapper\dists\apache-maven-3.8.4-bin being MyUser your user in the system for Windows.

also you can use the maven executor button in Intellij enter image description here

Hope it might be useful for someone


问:
Compare the characters of two strings and return the index of the character found
I'm trying to compare the characters of two Strings

String string1 = "ABC";

String string2 = "ABCCB";

so here for example if we compare the strings together , 'A' in string1 is found in index 0 in string1 'B' in index '1' and so long,so by comparing the strings I should get a number which is 01221, I know in java it has the contains() , indexOf() , but I can't think of how this can be achieved

答: Iterate over your string2 char by char and use string1.indexOf(c). You'll get the sequence of numbers.