473,385 Members | 1,445 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,385 software developers and data experts.

Help with this problem in C language!

14
I need to fix the following error in this program in C language.

The directions:

The following function supposedly computes the sum and average of the numbers in the array a, which has length n.avg and sum point to variables that the function should modify. Unfortunately, the function contains several errors; find and correct them.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void avg_sum(double a[], int n, double *avg, double *sum)
  4.     {
  5.         int i;
  6.          sum = 0;
  7.         for (i = 0; i < n; i++)
  8.          sum += a[i];
  9.          avg = sum / n; 
  10.     }
The part sum+= a[i]; gives me an error [Error] invalid operands `double *' and `int' to binary `operator \'
in the line [i]sum += a;

Thanks. Please advise.

PS. I am using the problem C - Free 4.1 to write the program in a Vista 64 bit system. If you have a better program, please don't hesitate to tell me.
Jun 18 '09 #1
6 3538
JosAH
11,448 Expert 8TB
In the body of that function replace every occurence of 'sum' by '*sum' and 'avg' by '*avg'. Both are pointers to doubles; you don't want to change the value of the pointers themselves, you want to change the doubles they point to.

kind regards,

Jos
Jun 18 '09 #2
donbock
2,426 Expert 2GB
@stayit
It is well worth your time to learn how to interpret error messages.

I am a little skeptical of the error message you gave us. Could it perhaps have been the following:
Expand|Select|Wrap|Line Numbers
  1. [Error] Invalid operands 'double *' and 'int' to binary operator '/'
and perhaps it was reported against line 9 instead of line 8?

Whenever possible you should cut the error message out of the log file and paste it into your post -- even a minor typo (like changing slash to backslash) can delay a helpful reply.

The error message tells you that the operands to the '/' operator are invalid; and that one of those operands is of type 'double *' and the other is of type 'int'. This should be enough to make you focus on the expression "sum / n". That's when you discover that the error message is right, sum is a 'double *'. As Jos said, you don't want to divide the pointer value by an integer, you want to divide the value being pointed at by an integer.

Bonus question: what happens if somebody calls avg_sum and passes a value of 0 for n?
Jun 18 '09 #3
stayit
14
Thanks, guys. That was very helpful. I think that by calling a 0 instead of n, it restricts the amount of numbers that you can average. At least, that was my initial thinking. Apologize, I am not very good at programming even though I have just a little experience in Java, Python.


@donbock
Jun 18 '09 #4
donbock
2,426 Expert 2GB
@stayit
I was hinting that your program fails dramatically if the caller sets n to zero -- and there is no compiler message to help you find this problem. You need to look at each spot where n is used and ask yourself what happens if it is zero.
Jun 18 '09 #5
But the logic is correct? when I use it in a main function, the math somehow returns incredibly wrong.
May 7 '14 #6
donbock
2,426 Expert 2GB
@petpenguin: the code in the original post is deeply flawed in its use of pointers.
May 8 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
8
by: Jose | last post by:
Can anyone help? I try to put all my function in a separate .ASP file under scriptlib folder but when I call any function from the such asp file do not work meanwhile if I put the code inside...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
2
by: Megat | last post by:
I'm trying to create a conversion program, that convert a simple proprietry programming language to an international standard languange, using Visual C++. Need some help from those who has...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
2
by: sachin | last post by:
Hello Everybody I need some help regarding Natural Language Processing. I am designing a MT system from a SOV language to a SOV language. I need a parser which can find the root word...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: ukpapillon | last post by:
Hi, I keep on getting an error message on my homepage. The header and footer disapear. Strangely if I select japenese on the language box on my homepage, everything goes to normal? Cannot find the...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
2
by: jayjayplane | last post by:
I want to insert value into one temp table, but based on the value from another table, like: select s2_sess_datetime,s2_individual_session from lop_2008_staging if s2_sess_datetime is not null...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.