answersLogoWhite

0

A row is just a one-dimensional array so, given a pointer to the first element of a row of doubles and the number of doubles in the row, we can use the following algorithm:

// returns the largest double in an array of size count

double get_largest (double * p, unsigned count) {

if (p==NULL size==0) { /* invoke invalid argument handler */ }

double result = *p; // store first value in row (dereference the pointer)

while (--count) { // repeat for the remainder of the row

++p; // advance to the next element

if (*p > result) result = *p; // if the current element is larger, update stored value

}

return result; // return largest value

}

User Avatar

Wiki User

7y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
ReneRene
Change my mind. I dare you.
Chat with Rene
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor

Add your answer:

Earn +20 pts
Q: C program to find the largest element in a row?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

The place in a spreadsheet where a row and a column intersect is called a?

A cell. In the periodic table an element fills that cell.


How do you initialize each element of a two-dimensional array alpha to 5?

Two-dimensional arrays are typically iterated through using nested for loops. If you had a 2-D array alpha with ints ROWS and COLS representing the number of rows and columns respectively, and ints row and col as iterators, the loop would look like this: for (row = 0; row < ROWS; row++){ for (col = 0; col < COLS; col++{ alpha[row][col] = 5; } }


How will you initialize a three-dimensional array thread 323. How will you refer the first and last element in this array?

The first element will be 0,0,0 and the last will be 2,1,2. It's always best to think of multi-dimensional arrays in terms of a one-dimensional array. That is, a 3x2x3 array is really a one-dimensional array of 3 elements (the first dimension), where each element is a 2x3 two-dimensional array, and where each of those 2x3 arrays is a one-dimensional array of 2 elements (the second dimension), where each element is a one-dimensional array of 3 elements (the third dimension). Thinking in these terms makes it much easier to visualise arrays with 4 or more dimensions. Arrays are always allocated contiguously, one element after the other. So the memory layout for your three dimensional array is as follows (where 000 means element 0,0,0): {{000, 001, 002}, {010, 011, 012}}, {{100, 101, 102}, {110, 111, 112}}, {{200, 201, 202}, {210, 211, 212}} The curly braces are merely notational, but they help to visualise the layout more clearly, dividing the memory into three groups of two groups of three elements. However, if we stack the three main groups one above the other it will be much easier to see visualise the structure: {000, 001, 002}, {010, 011, 012} {100, 101, 102}, {110, 111, 112} {200, 201, 202}, {210, 211, 212} If you look at each row you can clearly see that the first digit in each element is the same throughout that row. However, each row is really a two-dimensional array (of 2x3 elements), so each row is really a 2x3 table. The top row is therefore table 0, the middle row is table 1 and the bottom row is table 2. Let's look at table 0 more closely: Table 0: {000, 001, 002}, {010, 011, 012} Now let's stack the table's 2 elements so we can see the table more clearly: Table 0: {000, 001, 002} {010, 011, 012} Now the rows are identified by the second digit, where the top row is row 0 and the bottom row is row 1. Each row has three elements and the third digit identifies the column of that element (0, 1 or 2). Here's the complete structure: Table 0: Row 0: {000, 001, 002} Row 1: {010, 011, 012} Table 1: Row 0: {100, 101, 102} Row 1: {110, 111, 112} Table 2: Row 0: {200, 201, 202} Row 1: {210, 211, 212} From this we can clearly see that we can refer to any individual element through a table identifier (0, 1 or 2), a row identifier (0 or 1) and a column identifier (0, 1 or 2). Therefore to initialise this structure, we need to cycle through each identifier in turn. Here's how we do that using pseudo-code: given array A [3,2,3] for each table in 0 to 2 for each row in 0 to 1 for each column in 0 to 2 initialise A [table, row, column] next column next row next table


Find out the address of the element A452 where A is a 3-dimensional array with subscript limits 1 you 6 1 j 7 and 0 k 4 when A is in row major addressing Assume base address is 1000 and word size is 2?

1. Find out the address of the element A(4,5,2) where A is a 3-dimensional array with subscript limits 1≤ i ≤6, 1≤ j ≤7 and 0≤ k ≤4 when A is in row major addressing. Assume base address is 1000 and word size is 2.Answer:Array(depth,col,row) means A(4,5,2)Address= BASE+ ((depthindex*col_size+colindex) * row_size + rowindex) * Element_SizeADDRESS=1000+((4*7+5)*5+2)*2(1000+((33*5)+2)*2(1000+(167*2))(1000+334)1334


How do you write a program to find magic numbers?

#include<stdio.h> unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) { unsigned sum, col; sum = 0; for (col=0; col<width; ++col) sum += sq[row*width+col]; return sum; } unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) { unsigned sum, row; sum = 0; for (row=0; row<width; ++row) sum += sq[row*width+col]; return sum; } unsigned sum_diag (unsigned* sq, const unsigned width) { unsigned sum, row, col; sum = 0; for (row=0, col=0; row<width; ++row, ++col) sum += sq[row*width+col]; return sum; } unsigned sum_anti (unsigned* sq, const unsigned width) { unsigned sum, row, col; sum = 0; for (row=0, col=width-1; row<width; ++row, --col) sum += sq[row*width+col]; return sum; } bool is_magic (unsigned* sq, const unsigned width) { unsigned magic, row, col; magic = sum_row (sq, width, 0); for (row=1; row<width; ++row) if (magic!=sum_row(sq, width, row)) return false; for (col=0; col<width; ++col) if (magic!=sum_col(sq, width, col)) return false; if (magic!=sum_diag(sq, width)) return false; if (magic!=sum_anti(sq, width)) return false; return true; } int main () { const unsigned width = 3; unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}}; unsigned row, col; printf ("Square:\n\n"); for (row=0; row<width; ++row) { for (col=0; col<width; ++col) { printf ("%d ", a[row][col]); } printf ("\n"); } printf ("\n"); if (is_magic((unsigned*)&a, width)) printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0)); else printf ("The square is not magic\n"); return 0; }