Solved

# 1 line error code (average formula), please check. URGENT

Posted on 2011-10-21
227 Views
can someone check my code and see how can i fix the error on the formula?

The error comes in this line:
promedio=(d[cont].nota1)+(d[cont].nota2)+(d[cont].nota3)/3);

I have to calculate the average for 3 califications.

p.s. I am using DevC++
``````#include <stdio.h>

struct student
{
char nombre[50];    // name of the student
int anio[4];         // year
float nota1[3];	    // first calification
float nota2[3];	    // second calification
float nota3[3];	    // third calification
float promedio[8];  // THIS IS THE AVERAGE
char estado[1];     // APPROVED OR REJECTED?
};

int main(void)
{
struct student d[30];
int cont;
float average;

for(cont = 0; cont < 30; cont++)
{
printf("Name: ", &cont);
scanf("%s", d[cont].nombre);
printf("Ingrese Año Lectivo: ");
scanf("%s", &d[cont].anio);
printf("Ingrese Primera Nota del Certamen: ");
scanf("%d", &d[cont].nota1);
printf("Ingrese Segunda Nota del Certamen: ");
scanf("%d", &d[cont].nota2);
printf("Ingrese Tercera Nota del Certamen: ");
scanf("%d", &d[cont].nota3);

average=(d[cont].nota1)+(d[cont].nota2)+(d[cont].nota3)/3);
printf("The average is;,%.2f",average);
``````
0
Question by:José Perez

LVL 84

Expert Comment

parentheses are unbalanced.
there are three  left ( and four right )
0

LVL 2

Author Comment

for some reason it did not paste the first one, any way, it does no change, the error is the following:

invalid operands of types `float[3]' and `float[3]' to binary `operator+'
0

LVL 84

Expert Comment

d[cont].nota1 and d[cont].nota2 are arrays of three floats
+ does not work on arrays.
Did you want to select a particular element of the array?
0

LVL 2

Author Comment

The current element of the array, defined by "cont"

what would you suggest?
0

LVL 84

Expert Comment

d[cont] selects an element of the d array.
to get a float, you need to select an element of the nota1 array

I don't think you want to use cont to select from the nota1 array, since the nota1 array has only 3 elements,
and the value of cont can be greater than 2

But it's not clear why you'd want to declare nota1 as an array of floats, instead of as a float anyway.
0

LVL 2

Author Comment

Honestly I dont know how to do it, I just tried to do this way but it does not work. If you see i just want to calculate the average of nota1+nota2+nota3.

can you propose some way to fix this program?
0

LVL 2

Author Comment

I fix it!!!!!

the solution is doing this:

float nota1;
float nota2;
float nota3;
0

LVL 2

Author Comment

I've requested that this question be closed as follows:

Accepted answer: 0 points for oscargarin's comment http:/Q_27409811.html#37009925

for the following reason:

I found the answer by my self.
0

LVL 84

Accepted Solution

declaring nota1 a float instead of an array was one of the suggestions
0

LVL 13

Expert Comment

Note to moderator: I agree with Ozo.  While the author did solve the problem, he/she did so after Ozo posted sound assistance that was used in the solution.
0

LVL 2

Author Comment

ok. what is suggested in case an answer was part of the solution?
0

LVL 2

Author Comment

will it be fair to award 100 point each?
0

LVL 13

Expert Comment

It's not uncommon for an author to be involved in solving the problem.  IMH(unofficial)O the helper(s), in this case Ozo, should still credited for their effort.

If I asked the question, I would have awarded all the points.  (You can't actually award points to yourself anyway.)  But you should do what you feel is fair.

Whatever you do, don't give me any points.  I didn't help solve the problem.
0

## Featured Post

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

#### Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!