Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Someone with c++ knowledge please help

Name: Anonymous 2011-03-09 0:46

Im using visual c++ 2008 express edition from microsoft

Write a program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4x4 array. You need to rest two features :

-does each of the numbers 1,2,...,16 occur in the user input
-When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to eachother?

a magic square is when the sum of the elements in each row, column and in the two diagonals have the same value.

Name: Anonymous 2011-03-09 0:56

Do your own homework.

Name: Anonymous 2011-03-09 1:00

#include "implementation.h"

int main() {
    // Left as exercise for the reader
    implementation();
}

Name: Anonymous 2011-03-09 1:55

(define (magic-square? n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13)
    (= (+ n0 n1 n2 n3)
       (+ n4 n5 n6 n7)
       (+ n8 n9 n10 n11)
       (+ n12 n13 n14 n15)
       (+ n0 n4 n8 n12)
       (+ n1 n5 n9 n13)
       (+ n2 n6 n10 n12)
       (+ n3 n7 n11 n13)))


You do the rest.

Name: Anonymous 2011-03-09 2:03

>>4

(define (magic-square? n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13)
    (define (proper-number? n)
      (and (>= n 1) (<= n 16)))
   
    (andmap proper-number?
            (list n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12))
   
    (= (+ n0 n1 n2 n3)
       (+ n4 n5 n6 n7)
       (+ n8 n9 n10 n11)
       (+ n12 n13 n14 n15)
      
       (+ n0 n4 n8 n12)
       (+ n1 n5 n9 n13)
       (+ n2 n6 n10 n12)
       (+ n3 n7 n11 n13)
      
       (+ n0 n5 n10 n15)
       (+ n3 n6 n9 n12)))

Name: Anonymous 2011-03-09 2:10

*
(define (magic-square? n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13)
    (define (proper-number? n)
      (and (>= n 1) (<= n 16)))
    (and (array? n)
         (andmap proper-number?
                 (list n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12))
         (= (+ n0 n1 n2 n3)
            (+ n4 n5 n6 n7)
            (+ n8 n9 n10 n11)
            (+ n12 n13 n14 n15)
            (+ n0 n4 n8 n12)
            (+ n1 n5 n9 n13)
            (+ n2 n6 n10 n12)
            (+ n3 n7 n11 n13)
            (+ n0 n5 n10 n15)
            (+ n3 n6 n9 n12))))


Enjoy your troll ``LISP code'' without reading your ``SICP''.

Name: Anonymous 2011-03-09 2:11

>>6
(array? n)

What are you smoking?

Name: Anonymous 2011-03-09 2:12

>>6
>>7
*
(define (magic-square? n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13)
    (define (proper-number? n)
      (and (>= n 1) (<= n 16)))
    (and (andmap proper-number?
                 (list n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12))
         (= (+ n0 n1 n2 n3)
            (+ n4 n5 n6 n7)
            (+ n8 n9 n10 n11)
            (+ n12 n13 n14 n15)
            (+ n0 n4 n8 n12)
            (+ n1 n5 n9 n13)
            (+ n2 n6 n10 n12)
            (+ n3 n7 n11 n13)
            (+ n0 n5 n10 n15)
            (+ n3 n6 n9 n12))))

Name: Anonymous 2011-03-09 3:59


(define (proper-quotes? str)
  (and (string=? (substring str 0 2) "``")
       (string=? (substring str (- (string-length str) 2)) "''"))

Name: Anonymous 2011-03-09 6:34

mailto:nokooooo
fuuuuuck

Name: Anonymous 2011-03-09 7:20

>>7
Smoke lisp every ddy

Name: Anonymous 2011-03-09 9:30

>>11
My other ddy is an aay

Name: Anonymous 2011-03-09 15:46

Well, I'm still learning myself - but maybe to get you started:


using namespace std;
   
   int magic_square[4][4];
   int sum_rows[4] = {0, 0, 0, 0};
   int sum_cols[4] = {0, 0, 0, 0};
   int sum_diags[2] = {0, 0};
   
   cout << "Please enter 16 numbers:\n";
   
   // Collect user input
   for (int row = 0; row < 4; row++) {
      for (int col = 0; col < 4; col++) {
         while(!(cin >> magic_square[row][col])) {
            cin.clear();
            cin.ignore(10000, '\n');
            cout << "! Not a number, please try again:";        
         }
      }
   }
   
   // Display 4x4 table
   cout << "\nHere is the table:\n";
   for (int row = 0; row < 4; row++) {
      for (int col = 0; col < 4; col++) {
         cout << setw(8) << left << magic_square[row][col];
      }   
      cout << "\n";
   }

   // Sum of rows and columns 
   for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
         sum_rows[i] += magic_square[i][j];
         sum_cols[i] += magic_square[j][i];  
      } 
   }

   // Sum of diagonal from [0][0] to [3][3]
   for (int i = 0; i < 4; i++)
      sum_diags[0] += magic_square[i][i];
  
   // Sum of diagonal from [0][3] to [3][0]
   for (int i = 3, j = 0; i >= 0; i--, j++)
      sum_diags[1] += magic_square[i][j];
     
   // And... I'm too lazy to write something that compares all the values

Name: Anonymous 2011-03-09 15:53

>>13
`> mfw your input code is actually good
brofist

Name: Anonymous 2011-03-09 15:57

>>14
`> mfw
Syntax error: expected >mfw

Name: Anonymous 2011-03-09 15:57

C version


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

#define FALSE 0
#define TRUE 1

typedef int magic_square[16];

/* The magic sum is given by n * (n^2 + 1) / 2, n the order of magic square */
#define MAGIC_SUM 34

static int lines[10][4] = {
    /* Rows */
    {  0,  1,  2,  3 },
    {  4,  5,  6,  7 },
    {  8,  9, 10, 11 },
    { 12, 13, 14, 15 },
    /* Columns */
    {  0,  4,  8, 12 },
    {  1,  5,  9, 13 },
    {  2,  6, 10, 14 },
    {  3,  7, 11, 15 },
    /* Diagonals */
    {  0,  5, 10, 15 },
    {  3,  6,  9, 12 }
};

int is_valid_magic_square(magic_square ms)
{
    unsigned short bitmap;
    int sum;
    int i, j;

    for (i = 0; i < 16; i++)
    {
        if (ms[i] < 1 || ms[i] > 16)
            return FALSE;
        bitmap |= 1 << (ms[i] - 1);
    }

    if ((bitmap & 0xFFFF) != 0xFFFF) /* Have 1-16 */
        return FALSE;

    for (i = 0; i < 10; i++)
    {
        sum = 0;
        for (j = 0; j < 4; j++)
            sum += ms[lines[i][j]];
        if (sum != MAGIC_SUM)
            return FALSE;
    }

    return TRUE;
}

int main(int argc, const char * const *argv)
{
    magic_square ms;
    int i;
    int x;
    int ret;

    printf("Enter 16 numbers>\n");
    fflush(stdout);

    i = 0;
    while (i < 16)
    {
        ret = scanf("%d", &x);
        if (ret == 1)
            ms[i++] = x;
        if (ret == EOF)
        {
            perror(argv[0]);
            abort();
        }
    }

    if (is_valid_magic_square(ms))
        printf("Valid!\n");
    else
        printf("Invalid!\n");

    return 0;
}

Name: Anonymous 2011-03-09 16:04

>>13
Sepples lets you put code outside of functions?

Name: Anonymous 2011-03-09 18:51

>>16

#include <stdbool.h>

Don't change these.
Name: Email:
Entire Thread Thread List