Investigating Unexpected Outputs in C Code

In summary, the conversation is about trying to assign inputs to a particular output using code, but getting unexpected results. The code involves changing the value of rpm and using arrays to correspond to delay time for spark to occur. However, there were errors in the code such as using a C++ compiler for C and not accounting for the correct index in the array, leading to undefined behavior. Changes were suggested to fix the errors.
  • #1
akueddy
14
0
Hi,

I tried to assign inputs to a particular output and I've got some funny results. In this code
ive change the value of rpm to into the address of arrays that I've set. The adress correspond to the delay time for spark to occur.


Heres my c code :

Code:
#include <stdio.h>


void main()
{

int n,x;

    int Array[8] = { 5, 4, 3.5, 2, 1.5, 1,  0.5, 0};

    int *pArray;

printf("what is the RPM of the engine? 0-8000\n");
scanf("%d", &n);

x = n/1000 ;

pArray = &Array[x];



printf("Spark will be produce at : %d microseconds before top dead centre\n",*pArray);
}


And here's the output :

Code:
C:\Borland\BCC55\Bin>bcc32 pointers5
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
pointers5.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\Borland\BCC55\Bin>pointers5
what is the RPM of the engine? 0-8000
500
Spark will be produce : 5 microseconds before top dead centre

C:\Borland\BCC55\Bin>pointers5
what is the RPM of the engine? 0-8000
1000
Spark will be produce : 4 microseconds before top dead centre

C:\Borland\BCC55\Bin>pointers5
what is the RPM of the engine? 0-8000
7999
Spark will be produce : 0 microseconds before top dead centre

C:\Borland\BCC55\Bin>pointers5
what is the RPM of the engine? 0-8000
8000
Spark will be produce : 8000 microseconds before top dead centre

C:\Borland\BCC55\Bin>

My questions :

1) Why is the output's value equals to data stored in array 2 when clearly the input(1000) was suppose to point in array 1?

2) Why do i get 8000 microseconds when it is suppose to be 0 microseconds?
 
Technology news on Phys.org
  • #2
Here are some changes you need to make:
Code:
#include <stdio.h>


int main()  /* main always returns an int */
{

int n,x;

    float Array[8] = { 5, 4, 3.5, 2, 1.5, 1,  0.5, 0}; /* these are float numbers not int */

    float *pArray;  /* float pointer */

    printf("what is the RPM of the engine? 0-8000\n");
    scanf("%d", &n);
    x = n/1000 ;
    pArray = &Array[x];
    printf("Spark will be produced at : %f microseconds before top dead centre\n",
             *pArray); /* %d to %f */
    return 0;
}

When you divide two integers 7999/1000 you get 7 not 8.

arrays have indexes that start at 0, not 1. The second element of the array is array[1], the first element is array[0];

In the case pf 8000 - you went past the end of the array you looked at array[8], the ninth element - this is undefined behavior.

Undefined means anything (always bad) can happen. I think in this case the integer n was stored on the stack right after the end of the array. Also, consider getting a free modern C compiler. You're using a C++ compiler for C, which may also lead to unexpected results.
 
Last edited:
  • #3


I would suggest that you carefully review your code and check for any errors or logical mistakes. It is possible that you may have inadvertently assigned the wrong values to your arrays or made a mistake in your calculations. It is also important to double check your inputs and make sure they are correctly mapped to the corresponding arrays. Additionally, it may be helpful to use debugging tools to pinpoint the source of the unexpected outputs and troubleshoot any issues in your code. It is also important to thoroughly test your code with different inputs to ensure its accuracy and reliability.
 

Related to Investigating Unexpected Outputs in C Code

1. What are unexpected outputs in C code?

Unexpected outputs in C code refer to any results or behaviors that are not intended or predicted by the programmer. These can include errors, bugs, crashes, or incorrect results.

2. Why is it important to investigate unexpected outputs in C code?

Investigating unexpected outputs in C code is important because it helps identify and fix errors in the code. It ensures that the program functions as intended and produces accurate results. It also helps improve the overall quality and reliability of the code.

3. What are some common causes of unexpected outputs in C code?

Some common causes of unexpected outputs in C code include logical errors, syntax errors, memory leaks, uninitialized variables, and incorrect data types. These can occur due to mistakes in the code, improper use of functions or variables, or incorrect assumptions about how the code will be executed.

4. How can unexpected outputs in C code be investigated?

To investigate unexpected outputs in C code, one can use debugging tools such as a debugger or a compiler with debugging capabilities. These tools allow the programmer to step through the code and track the values of variables to identify where the unexpected output is occurring. Print statements can also be used to trace the flow of the program and identify potential issues.

5. How can unexpected outputs in C code be prevented?

To prevent unexpected outputs in C code, it is important to write clear and well-structured code that follows best practices. This includes using meaningful variable names, commenting code, and thoroughly testing the code before deployment. It is also helpful to use debugging tools during the development process to catch and fix any errors early on.

Similar threads

  • Programming and Computer Science
Replies
1
Views
983
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
669
  • Programming and Computer Science
Replies
21
Views
2K
Back
Top