473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

HELP!! Where am I going wrong?

I was recently given this assignment in my introduction to C
programming class. I have been working on this problem for hours and
I am at my wits end. The following code needs to be produced.
Write a function that finds and displays the maximum value in a

two-dimensional array of integers. The array should be declared as a
10-by-20 array of integers in main().

b. Modify this function so that it also displays the row and column
number of the element with the maximum value.<<<

So far I have developed the program up to this point:

#include <stdio.h>

int findMax(int[10][20]);

int main()
{
int val[10][20] =
{23, 54, 35, 11, 10, 94, 88, 56, 13, 14, 99, 63, 28, 94, 77, 45,
12, 19, 91, 76,
24, 55, 36, 16, 19, 95, 89, 57, 18, 34, 92, 64, 29, 91, 44, 32,
89, 11, 98, 88,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67};

printf("The maximum value is %d", findMax(val));
return 0;

}

int findMax(int nums[10][20])
{
int q, pass, temp;

for (pass = 0; pass <= 6; pass++)
{
for (q = 0; q <= 6; q++)
{
if(nums[q][q] > nums[q +1][q + 1])
{
temp = nums[q][q];
nums[q][q] = nums[q+1][q+1];
nums[q + 1][q + 1] = temp;
}
}
}
return(temp);
}

I cannot figure out for the life of me what I am doing or not doing to
make this program work. So I am calling on the gurus for a little
advice.

Thanks,

Shaun
Nov 13 '05 #1
1 1810
shauncarter1 wrote:

[snip - need help with homework]
Write a function that finds and displays the maximum value in a
two-dimensional array of integers. The array should be declared as a
10-by-20 array of integers in main().
qsort each row and then scan the last element of each row for the maximum value.
b. Modify this function so that it also displays the row and column
number of the element with the maximum value.
This is a bit more difficult if you didn't save the original matrix, but since
you've got the row, just scan that row for the value in the original matrix to
get the column. Otherwise...

[snip] int findMax(int nums[10][20])
This should probably be declared as

/* return the maximum value in n x m array nums */
int
findMax(int **nums, size_t n, size_t m)
{
int q, pass, temp;

for (pass = 0; pass <= 6; pass++)
Where does 6 come from?
{
for (q = 0; q <= 6; q++)
{
if(nums[q][q] > nums[q +1][q + 1])
{
temp = nums[q][q];
nums[q][q] = nums[q+1][q+1];
nums[q + 1][q + 1] = temp;
}
}
}
return(temp);
}


You will want to do something like

size_t i, j;
int max = 0;

loop over n
loop over m
update max if nums[i][j] > max
return max

or alternatively declare

typedef struct {
size_t row;
size_t col;
} Index;

and implement

/* return index of maximum value in n x m array nums */
Index findMax(int **nums, size_t n, size_t m);

HTH,

/david

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: M O J O | last post by:
Hi, I have a TextBox on my form. I capture the KeyDown event and examines the e.KeyValue - that is, I examine it like this: dim chr = chr(KeyValue). This seams to work ok .... well almost....
1
by: Vinodh | last post by:
Hi, I created an ASP.NET application in VB.NET. I put one text box (Textbox1) and a button (button1) to the web form (webform1). Then I clicked on the button and add the following code. ...
2
by: Jonas Cord | last post by:
Hi, I'm trying to learn proxy classes in C++ so I've written the following code, trying to modify an example given in Deitel & Deitel's C++ book. I am trying to "hide" details of the original...
4
by: Anne | last post by:
My competitors table has Memberid, lastname, first name, and I have three other fields (all number) year month and another for day. I decided to combine the year, month and day into one date...
6
by: Adam | last post by:
Hi everyone, I figured by doing a larger project, I could start to learn how objects and classes work in PHP, but I am running into a problem. I am trying to write a sudoku generator. I was...
15
by: E-Dot | last post by:
I am trying to write a program which asks the user to enter a number in the interval , the program then gives the natural logarithm of that number, using the series for log(x+1)... Here is what...
3
by: Sjef Janssen | last post by:
I try to get a tabbed display work both in IE (6 and 7) and Firefox. The last does give a problem. What's going wrong here? I'm totally puzzled. <!doctype html PUBLIC "-//W3C//DTD XHTML 1.0...
2
by: jonathan184 | last post by:
Hi basically what i want this script to do is In a particular dir there are alot of files from today back to 1999 or earlier. So I am trying to have the script search this dir and sort the files...
0
by: John Vines (CISD/HPCD) | last post by:
Yes spelling things correctly does help, got it thanks for the help.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.