Hey guys I am stuck with this program and i need help with it
this is what i got until now

#include <stdio.h>
#include "checkit.h"

double vectorDotProduct(double x1, double y1, double z1, double x2, double y2, double z2) 
{
	return(x1 * x2 + y1 * y2 + z1 * z2);
}
int main (void)
{
  int x,y,z,a,b,c;
  printf("Please enter the x, y, and z coordinates for the first vector: ");
  scanf("%f%f%f",&x,&y,&z);
  printf("Please enter the x, y, and z coordinates for the second vector: ");
  scanf("%f%f%f",&a,&b,&c);
  
  return 0;
}

this is for my hw but I am absolutely stuck on this one. Im trying to get help but its not working out.

Dani AI

Generated

As observed, the root cause is a type/format mismatch and the program never actually prints the function result. The safest repair is to choose one consistent representation for vector components and stick with it through input, computation and output. If the dot-product function uses doubles, declare the coordinates as doubles and read them with the double scanf specifier; alternatively, change the function to take integers if the exercise requires integer-only arithmetic.

A compact, robust pattern that avoids repeated separate variables is to store each vector in an array, read and validate input, then compute the dot product from those arrays. This also makes it easy to reuse the same read routine and to add validation for malformed input.

#include <stdio.h>

double dot(const double a[3], const double b[3]) {
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}

/* read_vector returns 1 on success, 0 on failure */
int read_vector(double v[3]) {
    return (scanf("%lf %lf %lf", &v[0], &v[1], &v[2]) == 3);
}

Additional practical notes: always check scanf's return value to detect bad input; compile with warnings enabled (for example, GCC: -Wall -Wextra) to catch format/argument mismatches; avoid leaving variables uninitialized; and keep any course-provided header (checkit.h) and prompt text exactly as required by the grader. For reference on format specifiers and their meanings see the C I/O documentation at cppreference.com.

Recommended Answers

All 3 Replies

Hey guys I am stuck with this program and i need help with it
this is what i got until now

#include <stdio.h>
#include "checkit.h"

double vectorDotProduct(double x1, double y1, double z1, double x2, double y2, double z2) 
{
	return(x1 * x2 + y1 * y2 + z1 * z2);
}
int main (void)
{
  int x,y,z,a,b,c;
  printf("Please enter the x, y, and z coordinates for the first vector: ");
  scanf("%f%f%f",&x,&y,&z);
  printf("Please enter the x, y, and z coordinates for the second vector: ");
  scanf("%f%f%f",&a,&b,&c);
  
  return 0;
}

this is for my hw but I am absolutely stuck on this one. Im trying to get help but its not working out.

A first look would suggest that you have declared variables a, b, c, x, y and z as integers and scanning in the scanf() function call for these variables as floats.

A first look would suggest that you have declared variables a, b, c, x, y and z as integers and scanning in the scanf() function call for these variables as floats.

but in class we havent used floats yet. I saw some examples on internet but they were too advanced for the class i am in right now. We're just in the basics. we went through only printf, int /double functions and scanf and if statements lol

but in class we havent used floats yet. I saw some examples on internet but they were too advanced for the class i am in right now. We're just in the basics. we went through only printf, int /double functions and scanf and if statements lol

Well then, change your scanf's as so:

scanf("%d%d%d",&x,&y,&z);

Also, in your code snippet, you are not calling the function - perhaps something like this may help:

printf("The result is %lf\n", vectorDotProduct(a, b, c, x, y, z));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.