I'm currently in a data structure class and i have little programing experience. The assignment is


A. In this part, you are supposed to design an integer random number generator that would produce random numbers between 10 and 99 (both inclusive). Design schemes to evaluate its performance as a random number generator. Comment on the output quality of your generator.

B. Consider the two dimensional array shown below. Write a program that would read in the array A and evaluate each of its magic square property using appropriate function calls

A=
11 24 7 20 3
4 12 25 8 16
17 5 13 21 9
10 18 1 14 22
23 6 19 2 15


Im currently working on part A but i was only able to generate the random numbers

Recommended Answers

All 4 Replies

Without any code to show I can only give you some hints:

For part A, I don't know how well your generator should perform. I suggest you first look up rand(), then if you want to improve your program follow up by researching srand() and then maybe mersenne twister or multiply-with-carry.

For part B, evaluate it how? You have a 5x5 square. so each row, column and diagonal sum should be 5(5^2+1)/2 or 65. So I'm guessing you maybe want a function that sums up 5 numbers to check if they equal 65. Then pass each row, column and diagonal into your function?

If you need more help, post some code and some more specifics such as how to evaluate the square and how random the numbers should actually be.

Thank you very much. my program is currently as shown :

#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;

main ()
{
        srand((unsigned)time(0));
        int i;
        int random [10000];
        for (i = 0; i <10000; i++)
                {
                        random [i] = (rand() % 89)+10;
                        cout<<random [i]<<endl;


                }
}

I have to upgrade this program so that it counts and records the amount of times each number from 10 to 99 shows up...

Ok, so then I suggest creating an array to hold the occurrences of each number
then every time a new random number is generated, increment the correct count.
then at the end you can print out the array.

Oh, and I modified your rand() statement to include the number 99 because it sounds like that is what you want.

int main ()
{
  srand((unsigned)time(0));
  int i;
  int random [10000];               // hopefully you're using this somewhere else
  int frequency[90] = { 0 };        // initialize all 90 counters to 0
  for (i = 0; i <10000; i++) {
    random [i] = (rand() % 90)+10;  // this means a number from 0 to 89 + 10
      frequency[random[i] - 10]++;  // increase counter of generated number
      cout<<random [i]<<endl;
  }

  for (i = 0; i < 90; i++) {        // loop through all occurrences
    cout << (i + 10) << " occurred " << frequency[i] << " times\n";
  }
}

if thats not exactly what you're after then modify it to get your desired outcome. Hopefully the comments explain it well enough

btw, if you aren't using that random[10000] anywhere else in your program then I suggest making it just a simple variable rather than an array and overwriting the previous contents on each iteration of the for loop

Thank you very much really appreciate it, part A works perfectly now. Im currently working on part B and created the code for the table. How should i show now that its a magic box? This is the code i have so far:

  UW PICO 5.01                                                                         File: labi2.cpp                                                                            

#include<iostream>
#include <cmath>
using namespace std;

const int ROW_SIZE = 5;
const int COLUMN_SIZE = 5;

main()
{
        int col, row;
        int matrix [5][5]= {{11, 24, 7, 20, 3},
                           {4, 12, 25, 8, 16},
                           {17, 5, 13, 21, 9},
                           {10, 18, 1, 14, 22},
                           {23, 6, 19, 2, 15}};






 for (row=0; row<ROW_SIZE; row++)
 { 
cout<<endl;
 for (col=0; col<COLUMN_SIZE; col++)    

    cout<<matrix[row][col]<<" ";      
cout<<endl;

 }      
}       

All i have left to do is the additions of the rows or columns and diagonals to prove a magic box. can i just add all the values one by one on a row or column or is there an easier way? thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.