C Function Help - Solve Issues with Void Function

  • MHB
  • Thread starter Colton0117
  • Start date
  • Tags
    Function
In summary, the conversation discusses a problem with implementing a void function in a program and the specific requirements for the function. The program should ask for user input, declare arrays, and call the function to compute the output array. The output should be displayed in the main function. There is a syntax error in the code for the insert0 function, but the main issue is still unresolved.
  • #1
Colton0117
5
0
I am having issues making my function work. If there is not a void function included, my program works as intended but sadly I need to have a function. I am at a loss as how to implement this type of function. The requirements are below and any help asap would be GREATlY appreciated!

Write a program that include the following function:
void insert0(int n, int a1[], int a2[]);

In the main function, ask the user to enter the length of the input array, declare the input and output arrays, read in the values for the input array, and call the insert0 function to compute the output array. The main function should display the result of the output array.
Enter the length of the array: 5
Enter the elements of the array: 3 4 9 1 4
Output: The output array is: 3 0 4 0 9 0 1 0 4 0

My code:

Code:
#include <stdio.h>

void insert0(int n, int a1[], int a2[]);

int main() {

int i = 0;
int n = 0;
int a1[n];
int a2[n * 2];

printf("Enter the length of the array: ");
scanf("%d",&n);

printf("Enter the elements of the array: ");

for(i = 0; i < n; i++){ //adds values to first array
        scanf("%d",&a1[i]);
}

//call function here

for( i = 0; i < n*2; i++){ //prints array 2
        printf("%d", a2[i]);
        }

void insert0 (int n, int a1[], int a2[]){//inserts 0's between each number
for(i = 0; i < n; i++){
        a2[i+i] = a1[i];
        a2[i+i+1] = 0;
        }
        return 0;
}
 
Last edited:
Technology news on Phys.org
  • #2
Colton0117 said:
I am having issues making my function work. If there is not a void function included, my program works as intended but sadly I need to have a function. I am at a loss as how to implement this type of function. The requirements are below and any help asap would be GREATlY appreciated!

Write a program that include the following function:
void insert0(int n, int a1[], int a2[]);

In the main function, ask the user to enter the length of the input array, declare the input and output arrays, read in the values for the input array, and call the insert0 function to compute the output array. The main function should display the result of the output array.
Enter the length of the array: 5
Enter the elements of the array: 3 4 9 1 4
Output: The output array is: 3 0 4 0 9 0 1 0 4 0

My code:

Code:
#include <stdio.h>

void insert0(int n, int a1[], int a2[]);

int main() {

int i = 0;
int n = 0;
int a1[n];
int a2[n * 2];

printf("Enter the length of the array: ");
scanf("%d",&n);

printf("Enter the elements of the array: ");

for(i = 0; i < n; i++){ //adds values to first array
        scanf("%d",&a1[i]);
}

//call function here

for( i = 0; i < n*2; i++){ //prints array 2
        printf("%d", a2[i]);
        }

void insert0 (int n, int a1[], int a2[]); //inserts 0's between each number   <------ This line is the problem
for(i = 0; i < n; i++){
        a2[i+i] = a1[i];
        a2[i+i+1] = 0;
        }
        return 0;
}

Look carefully at the function header, the marked line, for your insert0 function you have syntax error on the line.
 
  • #3
squidsk said:
Look carefully at the function header, the marked line, for your insert0 function you have syntax error on the line.

I have fixed that issue, although that was just a typo and not my actual problem.
 
  • #4
Colton0117 said:
I have fixed that issue, although that was just a typo and not my actual problem.

OK, then what's wrong with your solution, other than you haven't called the function in main?
 

FAQ: C Function Help - Solve Issues with Void Function

What is a void function in C?

A void function in C is a function that does not return a value. It is used to perform a specific task or operation without needing to return a result. It is denoted by the keyword "void" before the function name.

How do I pass parameters to a void function in C?

Parameters can be passed to a void function in C by including them in the parentheses after the function name. For example, if the function is called "printName" and takes in a string parameter, it would be declared as "void printName(string name)".

What types of issues can occur with void functions in C?

Some common issues that may occur with void functions in C include forgetting to include the "void" keyword in the function declaration, not passing in the correct number or type of parameters, or not properly using pointers when needed.

How can I debug issues with a void function in C?

To debug issues with a void function in C, you can use a debugger tool such as GDB to step through the code and see where the issue is occurring. You can also use printf statements to print out values and check if they are correct at different points in the function.

Can a void function be called from another function in C?

Yes, a void function can be called from another function in C. This allows for code reusability and organization, as multiple functions can call the same void function without needing to rewrite the code for that function. Additionally, a void function can also call other functions within it.

Similar threads

Back
Top