Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Doing calculations with arrays - invalid operands to binary

Posted on 2008-06-10
4
Medium Priority
?
659 Views
Last Modified: 2010-04-21
Hi,
I'm new to C programming but feel i'm pretty good at perl so I feel like I really should be able to do this.
I've looked at C tutorials and in several text books but I can't find anything which helps.

All I want to do is do some calculations with arrays of data.
for (i=1; i<=d; i++)
a[i] = b[i] / c[i];
Why does this give me the error message: invalid operands to binary /. I'm using Dev-C++.
The division seems to be causing a problem.
I've tried all data types. I want to use double or float.
I'm adding to an existing C program and earlier in the program there is this code:
for (i=1; i<=x; i++)
y[i] = z[i]  /  t;
This works but mine doesn't.

This type of thing is easy in perl but i've spent ages on this.

Can anyone tell me how to work through arrays and perform calculations on each element or point me to a useful website. Thanks


0
Comment
Question by:csnell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 200 total points
ID: 21749650
Hello csnell,

What you have written is fine and should work ... can you post complete code ...

Or you can try this small example.

#include <stdio.h>

int main ()
{
    float a[] = { 1.1, 2.2, 3.3 };
    float b[] = { .1, .2, .3 };
    float c[3];
    int i;

    for (i=0; i<3; i++)
        c[i] = a[i]/b[i];
    for (i=0; i<3; i++)
        printf ("%f\t",c[i]);
}

Regards,
sunnycoder
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21749651
First, array indexing in C starts at 0, not 1, so most likely :

        for (i=1; i<=d; i++)

has to be :

        for (i=0; i<d; i++)

where d is the length of the array.

About the division not working : can you show how you defined the arrays a, b and c ?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21749665
It should be more or less the same as Perl... try this...
#include <iostream>
 
int main()
{
	int a[] = {1,2,3,4,5};
	int b[] = {1,2,3,4,5};
 
	for(size_t i = 0 ; i < 5 ; ++i)
	{
		std::cout << a[i] << "/" << b[i] << "=" << a[i]/b[i] << std::endl;
		std::cout << a[i] << "*" << b[i] << "=" << a[i]*b[i] << std::endl;
		std::cout << a[i] << "+" << b[i] << "=" << a[i]+b[i] << std::endl;
		std::cout << a[i] << "-" << b[i] << "=" << a[i]-b[i] << std::endl;
	}
}

Open in new window

0
 

Author Closing Comment

by:csnell
ID: 31465662
Thanks for your help.
It wasn't working because when I had initialized the arrays I had used *. I had looked at what had been done previously in the program for what I thought were arrays. I now know they are pointers!. I have only just begun to understand the basics of pointers so I didn't notice my error.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question