0

Does anyone know how I can make a C program that assigns hexadecimal values to a whole row in a 4x4 array? Each row would have one value.(0x00,0xff, 0x55, and 0xff) It then needs to sum all of the elements which I can do.

#include "msp430g2553.h"

int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;     // Stop watchdog timer
//--------------------------------------------------------------------------
//char type is one byte; int type is two bytes; volatile to prevent optimization
//------------------------------------------------------------------------------

volatile unsigned int i=0, j=0, sum=0;  // sum is the sum of the indices
int Zeroes = 0x00, Ones = 0xff, Odds = 0x55, Evens = 0xaa;
unsigned char ArrayFill [4][4];

//------------------------------------------------------------------------------
//  Initialize ArrayFill to 0xff
//------------------------------------------------------------------------------
{
for (i=0; i<=3; i++)
    for (j=0; j<=3; j++)
        ArrayFill[i][j] = 0xff;
}
//------------------------------------------------------------------------------
//  Fill ArrayFill with the indices values and calculate the sum of the indices
//------------------------------------------------------------------------------
{
for (i=0; i<=3; i++)
    {
    for (j=0; j<=3; j++)
        {
        ArrayFill[i][j] = ????????;
        sum=??????;
        }
    }
}
sum = sum;
4
  • what have u tried so far? Commented Feb 20, 2017 at 4:36
  • Can you do initialization, or must it be assignment? Note that arrays cannot be assigned in C (unless the array is part of a structure). This makes assigning rows to an array hard. Most arrays can be initialized though — VLAs and dynamically allocated arrays can't be initialized formally. Commented Feb 20, 2017 at 4:41
  • That is where my problem comes from. The real names for the values above that I need to use are Zeroes, Ones, Odds, and Evens in that order and the compiler does not like that. I can use any method I want, but it doesn't like when I initialize them. Commented Feb 20, 2017 at 4:45
  • Have a look at this answer. Commented Jul 14, 2019 at 20:14

2 Answers 2

0

I would suggest you to initialize the hexadecimal values in unsigned char datatype.

For example:

unsigned char hex[4][4];
    for(i=0;i<4;i++){
            for(j=0;j<4;j++){

                    scan("%s",hex[i][j]);
            }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Could you do an example of a row with one of the values because these are coded not user input via a command window? It only needs the four above.
I have added what I have so far
0

Using valarray in C++ as shown below:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <valarray>

using namespace std;


int main()
{

volatile unsigned int i=0, j=0, sum=0, g_sum = 0;  // sum is the sum of the indices
int Zeroes = 0x00, Ones = 0xff, Odds = 0x55, Evens = 0xaa;
unsigned char ArrayFill [4][4] = { {0x00,0xff,0x55,0xaa}, {0x00,0xff,0x55,0xaa}, {0x00,0xff,0x55,0xaa}, {0x00,0xff,0x55,0xaa}};

cout << showbase // show the 0x prefix
     << internal // fill between the prefix and the number
     << setfill('0'); // fill with 0s

//    1. Initialization during declaration
for (i=0; i<=3; i++) {
    sum = 0;
    for (j=0; j<=3; j++) {
        sum += ArrayFill[i][j];
    }
    cout << "Row sum: " << hex << setw(12) << sum << endl;
}

//    2. Initialization using a staging variable
valarray<unsigned char> ArrayFill2 [4][4];
unsigned char init[] = {0x00,0xff,0x55,0xaa};
valarray<unsigned char> t(init,4);
for (i=0; i<=3; i++) {
    sum = 0;
    *ArrayFill2[i] = t;
    for (j=0; j<=3; j++) {
        //ArrayFill[i][j] = 0xff;
        sum += ArrayFill[i][j];
    }
    cout << "Row sum: " << hex << setw(12) << sum << endl;
}

//    3. Initializing using staging variable but chaing the type to match the type of sum
valarray<int> ArrayFill3 [4][4];
int init3[] = {0x00,0xff,0x55,0xaa};
valarray<int> t3(init3,4);
for (i=0; i<=3; i++) {
    sum = 0;
    *ArrayFill3[i] = t3;
    sum = (*ArrayFill3[i]).sum();
    cout << "Row sum: " << hex << setw(12) << sum << endl;
}

return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.