Formula throwing error 'called object is not a function' - c

I'm doing an exercise in C that has me inputting data into a struct and then manipulating it in a separate function. But when the program comes to the line where the actual math is being done, I get an error about a called object not being a function.
Here's the exact error:
p1s2.c:70:106: error: called object '(vectorArray + (sizetype)((unsigned int)i * 32u))->y * (vectorArray + (sizetype)((unsigned int)i * 32u))->y' is not a function
I'll apologize in advance for the code, this is a work-in-progress file so it isn't very clean.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Structure declaration.
struct vector {
double x; //X-coordinate for vector
double y; //Y-coordinate for vector
double z; //Z-Coordinate for vector
double length; //Length. Calculations will go here.
};
int howLong(struct vector *x);
int main(void)
{
int arraySize;
int i;
int vectorNum=1;
int retval; //Watch for counter in howLong.
int scanval; //Error checking for coordinates input (scanf statement)
printf("How many vectors would you like to calculate length for?\n");
scanf("%d", &arraySize);
//Allocate memory to struct.
struct vector *vectorArray = malloc(arraySize*sizeof(double));
printf("You will now enter the coordinates for %d vectors. \n", arraySize);
//Input loop.
for(i=0; i<=arraySize; i++){
printf("Please enter the X, Y, Z coordinates for vector %d. \n", vectorNum);
printf("Please separate the coordinates with spaces. \n");
int scanval; //Error checking: Scanval should be equal to three.
//scanf takes user input, converts to long float.
scanval=(scanf("%lf %lf %lf", &vectorArray[i].x, &vectorArray[i].y, &vectorArray[i].z));
if(scanval !=3) {
printf("You can't follow directions. That's too bad. \n");
exit(0);
}
//Print input back to user.
printf("Vector Number %d: %lf %lf %lf \n", vectorNum, vectorArray[i].x, vectorArray[i].y, vectorArray[i].z);
//Increment counters.
vectorNum++;
i++;
}
for(i=0; i<=arraySize; i++){
vectorArray[i].length=howLong(vectorArray);
i++;
}
}
Here's the external function howLong:
int howLong(struct vector *vectorArray)
{ //Function gets the struct and coordinate values, calculates length and writes to struct.
int i;
int vectorNum=1;
printf("Calculating vector length. \n");
//Math.
vectorArray[i].length = sqrt((vectorArray[i].x * vectorArray[i].x)+(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z));
//Error occurs on the line directly above this comment. ^^
printf("Length of Vector Number %d: %lf \n", vectorNum, vectorArray[i].length);
return vectorArray[i].length;
}
I don't get it. At first I thought it had to do with function names, but the error persisted after I changed the function name to howLong. Any ideas?

Well, here:
(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z)
is missing a plus sign, between the two () parts.

In function howLong
((vectorArray[i].x * vectorArray[i].x)+(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z));
^operator is missing

Related

I want to convert my array values from float to double to get smaller values

When I change the type of array from float to double (float array[][] to double array[][]), it doesn't scan the values correctly. All the values become zero. For example, if I enter 5 for input when it's float, it's 5.000000. However, when it's double, every value I enter is scanned as 0.0000000.
#include <stdio.h>
#include <math.h>
int main() {
int limestone, min=0;
//for entering height of array (number of limetstone)
printf("Enter number of limestone: ");
scanf("%d", &limestone);
float array[limestone][3];
//double array[limestone][3]; (the problem)
//for getting inputs
for(int i=0;i<limestone;i++)
{
printf("Enter the %d porosity, hydraulic conductivity (m/s), specific gravity: ", i+1);
scanf("%f %f %f", &array[i][0], &array[i][1], &array[i][2] );
}
//for print array (you can remove it)
for(int i=0;i<limestone;i++)
{
for(int j=0;j<3;j++)
{
printf("%f ",array[i][j]);
}
printf("\n");
}
//Comparing 3rd (Last) Column
for(int i=limestone-1;i>=0;i--)
{
if(array[i][2]<array[min][2])
{
min=i;
}
}
printf("The limestone with the lowest specific gravity is Limestone %d with a specific gravity of %f",min+1,array[min][2]);
return 0;
}
I got your code to work by changing your scanf line to
scanf("%lf %lf %lf", &array[i][0], &array[i][1], &array[i][2] );
All I did was change the %f's to %lfs.
This works for me at least, I think it's doing what you want

Infinite Recursion loop C

We have been learning about recursion vs iteration in C this week and we were required to make a program that recursively determines the value of the nth term of a geometric sequence defined by the terms a, ar, ar^2, ... ar^(n-q).
For the most part, I think I have it figured out, as it seems to display the correct values per run, but it doesn't manage to break the recursion when the tested value reaches zero. Also, if possible to get a better explanation of recursion, and some examples of when recursion would be preferred over iteration as I'm still struggling with the concept.
// 2/20/2018
//Lab 6 Solution for Page 369 PE 4 B
//including libraries to be used
#include <stdio.h>
#include <math.h>
int main() {
//Function prototype
double goAnswer(int *, double, double, double, double *, int);
//Declaring variables
int nValue = 0;
double ratio = 0;
double firstTerm = 0;
double answer = 0;
double addedAnswer = 0;
int count = 1;
//Setting up to ask for each value
printf("Please enter in the value of n: ");
scanf("%d", &nValue);
printf("Please enter in the ratio you'd like to use: ");
scanf("%lf", &ratio);
printf("Please enter in the first term to use: ");
scanf("%lf", &firstTerm);
addedAnswer = goAnswer(&nValue, ratio, firstTerm, answer, &addedAnswer,
count);
//Printing out the value of the first nth terms
printf("The value of all terms added together is: %lf\n", addedAnswer);
return 0;
}
//function header
double goAnswer(int *nValue, double ratio, double firstTerm, double answer,
double *addedAnswer, int count) {
if (nValue == 0){
return 0;
}
else{ //This part calculates the answer, prints the value to the screen,
adds the answer to a running sum, decreases the nValue by one and calls the
function again with the lower nValue
answer = firstTerm * pow(ratio, count);
printf("The value of term %d is: %lf\n", count, answer);
printf("This is the nValue: %d \n", *nValue);
*addedAnswer += answer;
nValue -= 1;
return (goAnswer(nValue, ratio, firstTerm, answer, addedAnswer,
(count + 1)));
}
}

Extra values when printing an array(converting from %s to %c)

I am trying to create a simple program where the user will have to enter a series of numbers and the program should output the square and the cube of the given number. However, when I try to use an array, it prints some random numbers I didn't even input. Any help would be appreciated to eliminate the unecessary input. Thank you.
#include <stdio.h>
int main()
{
char *value;
value = malloc(sizeof(20));
float answer;
int x;
int y;
scanf("%s" , value);
for(x=0; x < 20; x++)
{
y = value[x] - '0';
printf("\nThe square of %d is: %d" , y , y*y);
printf("\nThe cube of %d is: %d \n" , y , y*y*y);
}
return 0;
}
You are taking input in char and doing arithmetic operations on it.
Use this code, it will give you correct output.
#include <stdio.h>
int main()
{
int *value;
value = (int *)malloc(20 * sizeof(int));
//float answer;
int x;
int y;
for(x=0; x < 20; x++)
{
scanf("%d" , value + i);
}
for(x=0; x < 20; x++)
{
y = value[x];
printf("\nThe square of %d is: %d" , y , y*y);
printf("\nThe cube of %d is: %d \n" , y , y*y*y);
}
return 0;
}
The problem is with your malloc statement.
sizeof is used to determine the parameter size - in your case a hard-coded integer. The generated array is of size 4, which is exactly sizeof(20) instead of 20 integers which is 20*sizeof(int). It will be best to allocate the array statically if you know what size you need, see code below:
#include <stdio.h>
int main()
{
// This line sets value to an array of 20 ints
int value[20];
// Another, less favorable option, but still works:
// char *value = malloc(20 * sizeof(int))
float answer;
int x;
int y;
scanf("%s" , value);
for(x=0; x < 20; x++)
{
y = value[x] - '0';
printf("\nThe square of %d is: %d" , y , y*y);
printf("\nThe cube of %d is: %d \n" , y , y*y*y);
}
return 0;
}
The expression sizeof(20) returns the size of an int (the literal 20 is an int), which is typically only 4 bytes. In other words, you are only allocating a single integer for your array. All access outside of that single integer will result in undefined behavior.
You need to allocate sizeof(int) times the number of elements, if you want to dynamically allocate the memory. Or (which I recommend) use a normal array:
int value[20];
There is also another problem, in that you only read a single value from the user. You should probably be reading in the loop too.
But if you read in the loop, then there is really no need to even have an array to begin with, only a single int Variable which you read into, and then print its value as squared and cubed.
So the code could be simplified as
#include <stdio.h>
int main(void)
{
int value;
for (unsigned i = 0; i < 20 && scanf("%d", &value) == 1; ++i)
{
printf("The square of %d is: %d\n", value, value * value);
printf("The cube of %d is: %d\n", value, value * value * value);
}
return 0;
}
You also need to be careful of overflows when multiplying.

Function not returning value to other function (C)

I'm writing this program for class in which I have to compute the equivalent resistance in series.
The description of the Homework is:
"The present homework is concerned with the use of pointers in computing the equivalent resistance to resistors connected in series. The number of resistors N connected in series and their resistance values R[0], R[1], …, R[N-1] are user-supplied...the inputting of the number of resistors N and the one-dimensional array elements R[0], R[1], …, R[N-1] is done within the “input” prototype function...these values in turn are passed to the “series” prototype function, where the computation of the equivalent resistance Req is performed... the input and output values are outputted to the console from within the “input” function.
-The prototype functions “input” and “series” are to be both placed before the “main” function, with the prototype function “series” placed between the “input” and “main” functions."
Code:
#include <stdio.h>
#define x 100
#define y 10000
float series(int N, float R[]);
void input() {
printf("\n---------------Compute equivalent resistance in series!---------------\n");
int N;
float R[y];
printf("\nPlease enter amount of resistors: \n");
scanf_s("%d", &N);
for (int i = 1; i <= N; i++) {
printf("\nEnter resistance for resistor %d: \n", i);
scanf_s("%f", &R[i]);
}
series(N, R);
printf("\n");
for (int i = 1; i <= N; i++) {
printf("The resistance of R[%d] is: %.2f.\n", i, R[i]);
}
printf("\nThe equivalent resistance is: %.2f Ohms.", Req);
}
float series(int N, float R[]) {
float Req = 0;
for (int i = 1; i <= N; i++) {
Req += R[i];
}
return Req;
}
int main() {
input();
getchar();
return 0;
}
My problem is that Req isn't being returned to the 'input' function in order to output the Equivalent resistance. Please help. Thank You
You're never assigning the result of series to a variable.
float req_ret;
req_ret = series (N, R);

C programming return value odd

I tried to search this everywhere, but it's kind of difficult to word, it's most likely a simple fix. Basically when I go through my program that is supposed to compute the average rainfall for a year, it comes out with a very large number, however, I thought it may have been just that I was doing the arithmetic wrong or had a syntax error of some sort, but that was not the case, when I checked the value that the function returned it was the proper value.
#include <stdio.h>
#include <string.h>
void getData(float *, float *);
int main()
{
char state[2], city[81];
float rainFall[12], outputAverage, *pAverage;
printf("Name Here\n");
printf("Please enter the state using a two letter abreviation: ");
gets(state);
printf("Please enter the city : ");
gets(city);
pAverage = &outputAverage;
(getData(rainFall, pAverage));
printf("%.2f", outputAverage);
return (0);
}
void getData(float *rainFall, float *pAverage)
{
int i;
float total;
for (i=0; i<12; i++)
{
printf("Please enter the total rainfall in inches for month %d: ", i+1);
scanf("%f", &rainFall[i]);
total += rainFall[i];
}
*pAverage = total / 12;
}
you need to initialize total
float total = 0.0;
Initialize the total to 0
Why you make it complicated? Why not just
return total / 12 ?
and called it like
outputAverage = getData(rainfall)
This is a classic problem in C programming. You are mixing strings and numbers on the input. You are better off reading the input into a string and then, using sscanf to parse it properly.
You have uninitialized variable total which is taking garbage value, thus you see a very large answer.
changed your main.. have a look and let me know if you have understood what changes i have made?
#include <stdio.h>
#include <string.h>
void getData(float *);
int main(int argc, char*argv[])
{
char state[3]={0}, city[81]={0};
float outputAverage;
printf("Name Here\nPlease enter the state using a two letter abreviation: ");
scanf("%s",state);
printf("Please enter the city : ");
scanf("%s",city);
getData(&outputAverage);
printf("The Average Rainfall recorded for the year is %.2f\n", outputAverage);
return 0;
}
void getData(float *pAverage)
{
int i;
float rainFall[12]={0}, total=0;
for (i=0; i<12; i++)
{
printf("Please enter the total rainfall in inches for month %d: ", i+1);
scanf("%f", &rainFall[i]);
total += rainFall[i];
}
*pAverage = total / 12;
}
However instead of using gets you should use fgets but i forgot how to counter the issue of using simultaneous fgets to read input from the standard input stream.
Also initialize the total variable as you are adding in the loop new values to existing value in that variable which would not necessarily add to zero as the premier element. so it could be any garbage value + loop values.
I understand you are practicing pointer concept so you passed the address of the array of floats to your second function but if the rainfall function is not useful in main, Better to restrict the same where it would be useful

Resources