MHB C Function Help - Solve Issues with Void Function

  • Thread starter Thread starter Colton0117
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
SUMMARY

The discussion revolves around implementing a void function in C to insert zeros between elements of an input array. The required function, void insert0(int n, int a1[], int a2[]);, is intended to be called from the main function after the user inputs the array length and its elements. The user provided code contains a syntax error in the function declaration and fails to call the function correctly within the main function. The output array should display the original elements interspersed with zeros, as demonstrated in the example output.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of arrays in C
  • Function declaration and definition in C
  • Input/output operations using scanf and printf
NEXT STEPS
  • Review C function syntax and correct function calls
  • Learn about dynamic array allocation in C
  • Explore array manipulation techniques in C
  • Understand the use of pointers in C for array handling
USEFUL FOR

C programmers, students learning about functions and arrays, and developers looking to improve their understanding of array manipulation in C.

Colton0117
Messages
5
Reaction score
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
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.
 
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.
 
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?
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
Replies
47
Views
5K
Replies
7
Views
2K