CProgramming Does anyone know a way to assign values to a wh
C-Programming. Does anyone know a way to assign values to a whole row in an array in C. The array is a 4x4 and the hexadecimal values are 0x00,0xff,0x55,0xaa. Each row would have one of these values. The code also needs to sum all of the elements in the array.
Solution
Modified:
#include<stdio.h>
 int main()
 {
    int columns=4, rows=4, sum=0; //-- assigned rows and columns as 4, since it\'s as 4x4 matrics array
    //unsigned int hex[rows][columns];
    unsigned int hex[4][4] = {0x00, 0x02, 0x03, 0x04, 5, 0xff, 0x07, 0x08, 0x09, 0x10, 0x11, 0x55, 0x13, 0x14, 0x15, 0xaa}; //-- assign hexadecimal values
   for(rows=0; rows<=3; rows++)
    {
        for(columns=0;columns<=3;columns++)
        {
           //printf(\"Enter value for hex[%d][%d]:\", rows, columns); //-- read the value, both decimal and hexadecimal
           //scanf(\"%x\", &hex[rows][columns]);
           sum += hex[rows][columns]; //-- sum of the array element
        }
     }
    printf(\"Sum of the array element = %x\ \", sum); //-- print the result of sum of the element
     return 0;
 }
 OUTPUT:
 Sum of the array element = 281
==================================
#include<stdio.h>
 int main()
 {
    int columns=4, rows=4, sum=0, sum1=0; //-- assigned rows and columns as 4, since it\'s as 4x4 matrics array
    unsigned int hex[rows][columns];
   for(rows=0; rows<=3; rows++)
    {
        for(columns=0;columns<=3;columns++)
        {
           printf(\"Enter value for hex[%d][%d]:\", rows, columns); //-- read the value, both decimal and hexadecimal
           scanf(\"%x\", &hex[rows][columns]);
           sum += hex[rows][columns]; //-- sum of the array element
        }
     }
    printf(\"Sum of the array element = %x\ \", sum); //-- print the result of sum of the element
     return 0;
 }
OUTPUT:
 Enter value for hex[0][0]:0x00
 Enter value for hex[0][1]:2
 Enter value for hex[0][2]:3
 Enter value for hex[0][3]:4
 Enter value for hex[1][0]:5
 Enter value for hex[1][1]:0xff
 Enter value for hex[1][2]:7
 Enter value for hex[1][3]:8
 Enter value for hex[2][0]:9
 Enter value for hex[2][1]:10
 Enter value for hex[2][2]:11
 Enter value for hex[2][3]:0x55
 Enter value for hex[3][0]:13
 Enter value for hex[3][1]:14
 Enter value for hex[3][2]:15
 Enter value for hex[3][3]:0xaa
 Sum of the array element = 281
Here the input value is hexadecimal and it returns the sum of value is also hexadecimal,
 it will add like,
 hex = dec
 0x00 = 0
 0xff = 255
 10   = 16
 11   = 17
 0x55 = 85
 13   = 19
 14   = 20
 15   = 21
 0xaa = 170
 0+2+3+4+5+255+7+8+9+16+17+85+19+20+21+170 = 641 this is the decimal value.
 281 is a hexadecimal value.


