Question

Data Structures and Algorithms Problem **HELP** I translated my code from C++ to Java to learn..Please...

Data Structures and Algorithms Problem **HELP**

I translated my code from C++ to Java to learn..Please make all necessary corrections in JAVA :)

Programming problem:

Write a program, using your favorite computer (under some operating system, supporting VMM) and your favorite programming language, to implement the algorithm on p. 126 for n= 16, 64, 256, 1024, 4096, and 16384, and for two values for m, m= 1677721600 and m= 13421772800 (that is m does not depend on n). Determine the timings for your twelve instances. Carefully discuss and interpret your results! What should be the computational complexity of the twelve runs?

Code:

package TimingAlgorithm

public class values {

    public static void main(string[] args){

        //n-values to be used

        int n[6] = { 16, 64, 256, 1024, 4096, 16384 };
        int next = 0;
        while (next < 6) {
            int a, b, x;
            //initialize M
            int**M = new int *[n[next]];
            //for loop for(int i = 0; i < n[next]; i++){

            M[i] = new int[i];
        }
        // Initialize Matrix to 0
        //For Loop

        for(int i = 0; i < n[next]; i++){
            //for loop
            for(int j = 0; j < n[next]; j++){
                M[i][j] = 0;
            }
        }
        //size_t m = 1677721600
        size_t m = 13421772800;
        time_t timer;
        timer = clock();
        //Calculation by adding random numbers from 1-100
        for(int i = 0; i < m; I++) {
            x = rand() % 100 + 1;
            b = rand() % n[next];
            a = rand() % n[next];
            M[a][b] = M[a][b] + x;
        }
        timer = clock() - timer
        System.out.println("Clock time for n size %d: %.2f seconds" + n[next], ((float)timer)/CLOCKS_PER_SECOND);
        System.out.println("\n\n");
        next++; //increment
        }
    }

How is this problem not properly explained?

Just write/correct the code of the program per the instructions. Don't worry about the timings, complexities, just the program :)

Here is the code in C++

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

using namespace std;

int main() { //n-values to be used int n[6] = { 16, 64, 256, 1024, 4096, 16384 }; int next = 0; while (next<6) { int a, b, x; //initialize M int** M = new int*[n[next]]; for (int i = 0; i < n[next]; i++) { M[i] = new int[i]; } //Initialize the Matrix to 0 for (int i = 0; i < n[next]; i++) { for (int j = 0; j < n[next]; j++) { M[i][j] = 0; } } //size_t m = 1677721600 size_t m = 13421772800; time_t timer; timer = clock(); //perform the calculation by adding random numbers from 1-100 for (int i = 0; i < m; i++) { x = rand() % 100 + 1; b = rand() % n[next]; a = rand() % n[next]; M[a][b] = M[a][b] + x; }
Pio Estepa 0883903 Data Structure and Algorithms HW #2
timer = clock() - timer; printf("Clock time for n size %d: %.2f seconds", n[next], ((float)timer) / CLOCKS_PER_SEC); cout << endl; next++; } return 0; }

Please proof my work. I intend to cite everything I use and my results and discussion per the requirements of this problem. Just make sure the code is right and it runs/debugs please!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1


I debugged all your code.
your code you did not mention m.
and what is size_t m;
please check that.
i have solved another problem. you have done really great conversion.
at someplace you are using M and m please also check that because I am not sure about your output. if size_t is default function then also check it. Because in some compiler it not work as well as Clocks_per_second. rest code is perfectly correct according to your cpp code.


package TimingAlgorithm

public class values {

public static void main(string[] args){

//n-values to be used

int n[6] = { 16, 64, 256, 1024, 4096, 16384 };
int next = 0;
while (next < 6) {
int a, b, x;
//initialize M
int** M = new int *[n[next]];
//for loop
for(int i = 0; i < n[next]; i++)
{
M[i] = new int[i];
}
// Initialize Matrix to 0
//For Loop

for(int i = 0; i < n[next]; i++){
for(int j = 0; j < n[next]; j++){
M[i][j] = 0;
}
}
size_t m = 1677721600;
size_t m = 13421772800;
time_t timer;
timer = clock();
//Calculation by adding random numbers from 1-100
for(int i = 0; i < m; i++) {
x = rand() % 100 + 1;
b = rand() % n[next];
a = rand() % n[next];
M[a][b] = M[a][b] + x;
}
timer = clock() - timer
System.out.println("Clock time for n size %d: %.2f seconds" + n[next], ((float)timer)/CLOCKS_PER_SECOND);
System.out.println("\n\n");
next++; //increment
}

Add a comment
Know the answer?
Add Answer to:
Data Structures and Algorithms Problem **HELP** I translated my code from C++ to Java to learn..Please...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT