C function and arrays - c

I'm learning C programming and passing arrays to multiple functions, not sure when and why my array is overwritten by something, help me debug and spot my mistake in code:
#include <stdio.h>
int insertNumbers(int *numbers, int howManny){
int i;
for(i=0; i< howManny; i++){
printf("Insert number:");
scanf("%d", &numbers[i]);
}
printf("\nNumbers :(insertNumbers function)\n");
for (int i = 0; i < howManny; ++i) {
printf("%d: %d\n",i, numbers[i]);
}
return *numbers;
}
int add(int *numbers, int howManny){
int sum = 0;
for (int i = 0; i < howManny; ++i) {
sum = sum + numbers[i];
}
return sum;
}
void printArray(int *numbers, int howManny){
printf("\nNumbers:(print array function)\n");
for (int i = 0; i < howManny; ++i) {
printf("%d: %d\n",i, numbers[i]);
}
}
int main(){
int numbers, howManny, sum = 0, numbersArray;
printf("How manny numbers do you want?");
scanf("%d", &howManny);
numbersArray = insertNumbers(&numbers, howManny);
sum = add(&numbers, howManny);
printf("Total sum is: %d",sum);
printArray( &numbersArray, howManny);
return 0;
}
result is
How manny numbers do you want?3
3
Insert number:10
10
Insert number:20
20
Insert number:30
30
Numbers :(insertNumbers function)
0: 10
1: 20
2: 30
Total sum is: 60
Numbers:(print array function)
0: 10
1: 3
2: 10
looks like my array is overwritten somewhere but not sure when and why
even when I try to use
printArray( &numbers, howManny);
still not working but getting 10, 10, 50 values

Your array has not been created at the compile time. Also, the array size is given at the runtime of your program. Therefore, the array cannot be static so it has to be allocated dynamically and freed at the end, in order to work properly.
That being said, here I wrote a simple solution for what you're trying to achieve.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void insertNumbers(int *numbers, int howManny){
int i;
for(i=0; i< howManny; i++){
printf("Insert number:");
scanf("%d", &numbers[i]);
}
printf("\nNumbers :(insertNumbers function)\n");
for (int i = 0; i < howManny; ++i) {
printf("%d: %d\n",i, numbers[i]);
}
}
int add(int *numbers, int howManny){
int sum = 0;
for (int i = 0; i < howManny; ++i) {
sum = sum + numbers[i];
}
return sum;
}
void printArray(const int *numbers, int howManny){
printf("\nNumbers:(print array function)\n");
for (int i = 0; i < howManny; ++i) {
printf("%d: %d\n",i, numbers[i]);
}
}
int main(){
int *numbers, howManny;
printf("How manny numbers do you want?");
scanf("%d", &howManny);
// allocate memory (dynamic allocation)
numbers = (int *)malloc(sizeof(int) * howManny);
// validate memory allocation
assert(numbers != NULL);
// numbers is modified and returned by reference
insertNumbers(numbers, howManny);
printf("Total sum is: %d", add(numbers, howManny));
printArray(numbers, howManny);
// free dynamic allocation
free(numbers);
return 0;
}

Related

why my outer loop is not repeating itself?

#include<stdio.h>
void printarr( int arr , int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d and %d is :%d\n",z+1 ,
x+1 , x+2 , arr);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", marks[i][j]);
}
}
printarr(marks , 5 , 2);
return 0;
i am getting to put only two times and the outer loop is not repeating itself
please explain me in simple terms , i am just learning this launguage
complete begineer.
1.When passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified.
void printarr( int arr , int a,int b){ --->void printarr( int arr [][2], int
a,int b){
2.When reading array element in scanf you have missed &
scanf("%d", marks[i][j]);--->scanf("%d", &marks[i][j]);
3.When Printing array elements in printf you have to specify the index.
arr ---> arr[i][j]
#include<stdio.h>
void printarr( int arr [][2], int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d is :%d\n",z+1,x+1,arr[z][x]);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", &marks[i][j]);
}
}
printarr(marks, 5 , 2);
return 0;
}
You want this: (explanation in comments)
#include <stdio.h>
#include<stdio.h>
void printarr(int a, int b, int arr[a][b]) { // pass an array, not an int,
// and a and b must be before arr
for (int z = 0; z < a; z++) {
for (int x = 0; x < b; x++) {
printf("the marks of student %d in subject %d and %d is :%d\n", z + 1,
x + 1, x + 2, arr[z][x]); // use arr[x][z] here. arr obviously dons' make sense
}
}
}
int main() {
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i = 0; i < n_students; i++) {
for (int j = 0; j < n_sub; j++) {
printf("enter the marks of student %d in subject %d\n", i + 1, j + 1);
scanf("%d", &marks[i][j]); // you forgot the &
}
}
printarr(5, 2, marks); // you need to pass the size beforen the array
return 0; // read the documentation about VLAs
}

Printing array elements as difference between the max value of the array

I wrote a program to get an array which will return an array with elements as difference between max value and remaining elements.
#include <stdio.h>
void behind(int *, int);
int main(void) {
int array[10];
int N, i;
scanf("%d", &N);
for (i=0; i<N; i++) {
scanf("%d", &array[i]);
}
behind(array, N);
for (i=0; i<N; i++) {
printf("%d\n", array[i]);
}
return 0;
}
void behind(int *ptr,int size) /* Write your function behind() here: */
{
int i,max=0;
for(i=1;i<size;i++){
if(ptr[i-1]>=ptr[i])
max=ptr[i-1];
else
max = ptr[i];
}
for(i=0;i<size;i++);
ptr[i]=max-ptr[i];
}
why my program always return the same array that I got as input?

Functions and arrays which allows you to print what's the most repeated number

I have a problem with my code, it doesn't print the result I expect. This code allows the user to enter as many numbers as he wishes and then print the most repeated one
Here it is:
#include <stdio.h>
void reading_numbers(int array[]){
int i = 0;
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
}
void most_present_number(int array[], int Max){
int i = 0;
reading_numbers(array);
int current_number = array[i];
int current_number_count = 0;
int most_present_one = 0;
int most_present_one_counter = 0;
while (i < Max) {
if (array[i] == current_number) {
current_number_count++;
i++;
} else {
if (current_number_count > most_present_one_counter){
most_present_one = current_number;
most_present_one_counter = current_number_count;
}
current_number_count = 1;
}
}
printf("This is the most present number %d it is repeated %d times\n", most_present_one,
most_present_one_counter);
}
int main() {
int Max = 0;
int array[Max];
most_present_number(array, Max);
return 0;
}
The problem for me is when I call the function, but I don't know how to fix it
I should have written as a premise but I'm a bit new to C so probably there are some things in this code that don't make sense
I make a procedure to find the result ,int main() must have the size of array (mistake logic),because the procedure take the parameters of the main function (int main ())
Here is my code:
#include<stdio.h>
void most_present_number(int Max,int T[Max])
{
int i = 0;
while (i < Max)
{
printf("Insert the numbers :");
scanf("%d", &T[i]);
i++;
}
int k=0,cpt1=0,cpt=0;
for(int i=0;i<Max;i++)
{
cpt=0;
for(int j=i+1;j<Max;j++)
{
if(T[i]==T[j])
{
cpt++;
}
}
if(cpt>=cpt1)
{
cpt1=cpt;
k=T[i];
}
}
printf("This is the most present number %d it is repeated %d times\n",k,cpt1+1);
}
int main()
{
int Max = 0;
do
{
printf("How much long the array will be?\n");
scanf("%d", &Max);
}while(Max<1);
int T[Max];
most_present_number(Max,T);
}
the following proposed code:
cleanly compiles
performs the desired functionality
only includes header files those contents are actually used
and now the proposed code:
#include <stdio.h>
void reading_numbers( int Max, int array[ Max ][2])
{
for( int i = 0; i < Max; i++ )
{
printf("Insert the numbers\n");
scanf("%d", &array[i][0]);
array[i][1] = 0;
}
}
void most_present_number( int Max, int array[ Max ][2] )
{
for( int i=0; i < Max; i++ )
{
for( int j=i; j<Max; j++ )
{
if ( array[i][0] == array[j][0] )
{
array[i][1]++;
}
}
}
int most_present_one = array[0][0];
int most_present_one_counter = array[0][1];
for( int i=1; i<Max; i++ )
{
if( most_present_one_counter < array[i][1] )
{
most_present_one = array[i][0];
most_present_one_counter = array[i][1];
}
}
printf("This is the most present number %d it is repeated %d times\n",
most_present_one,
most_present_one_counter);
}
int main( void )
{
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
int array[Max][2]; // uses variable length array feature of C
reading_numbers( Max, array );
most_present_number( Max, array );
return 0;
}
a typical run of the code:
How much long the array will be?
4
Insert the numbers
1
Insert the numbers
2
Insert the numbers
3
Insert the numbers
2
This is the most present number 2 it is repeated 2 times

How can I manipulate an Array?

I have an array
arr[]={7,5,-8,3,4};
And I have to update the same array to
arr[]={7,12,4,7,11};
my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int sumArr(int *arr, int size);
void main()
{
int arr[] = { 7,5,-8,3,4 };
int i, size, res = 0;
printf("Enter Size Of The Array:");
scanf("%d", &size);
res = sumArr(arr, size);
for (i = 0; i < size; i++)
{
printf("%d\n", res);
}
}
int sumArr(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
arr[i+1]+= arr[i];
printf(" %d \n", arr[i + 1]);
}
return arr[i+1];
}
The output should be: 7,12,4,7,11
But in my code, the output is: 12,4,7,11,-858993449,58196502,58196502,58196502,58196502,58196502
Any hints?
I can use auxiliary functions for input and output arrays, will it help?
You have several mistakes in your code:
You need to stop the summing loop once i+1 reaches the end of the array
Your code knows the size; there is no need to read it from end-user
You need to print the value of res once, rather than printing it in a loop
You should consider moving the printing portion of the program into main from sumArray.
The modifications are very straightforward:
int sumArr(int *arr, int size) {
// Stop when i+1 reaches size; no printing
for (int i = 0; i+1 < size; i++) {
arr[i+1]+= arr[i];
}
return arr[size-1];
}
Printing in the main:
printf("sum=%d\n", res);
for (int i = 0; i < size; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
Demo.

Function call in C with array

I have a function that takes array 1 and copies/manipulates it to array 2. Basically what it does is take the user input in array one, lets say (2, 3, 3) and array 2 is stored as (2, 0, 3, 0, 3). I know this works because it worked without implementing a function but sadly I have to have one. I cannot for the life of me figure out how to call the function, I believe I don't need a return since its a void and not returning a value. Below is my code any help would be appreciated.
#include <stdio.h>
void insert0(int n, int a1[], int a2[]);
int main() {
int i = 0;
int n = 0;
int a1[n];
int a2[2*n];
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]);
}
insert0(); //call function which is wrong and I cannot get anything to work
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;
}
}
}
Modifying n after declaraing a1 and a2 won't magically increase their size. Declare a1 and a2 after reading the size into n to use variable-length arrays.
You must pass proper arguments to call insert0.
Defining functions inside functions is GCC extension and you shouldn't do that unless it is required.
a2 should have n*2 - 1 elements, not n*2 elements.
After moving it out of main(), i is not declared in insert0, so you have to declare it.
You should check if readings are successful.
Corrected code:
#include <stdio.h>
void insert0(int n, int a1[], int a2[]);
int main() {
int i = 0;
int n = 0;
printf("Enter the length of the array: ");
if(scanf("%d", &n) != 1){
puts("read error for n");
return 1;
}
if(n <= 0){
puts("invalid input");
return 1;
}
int a1[n];
int a2[2*n-1];
printf("Enter the elements of the array: ");
for(i = 0; i < n; i++){ //adds values to first array
if(scanf("%d", &a1[i]) != 1){
printf("read error for a1[%d]\n", i);
return 1;
}
}
insert0(n, a1, a2);
for( i = 0; i < n*2-1; i++){ //prints array 2
printf("%d", a2[i]);
}
}
void insert0 (int n, int a1[], int a2[]){ //inserts 0's between each number
int i;
for(i = 0; i < n; i++){
a2[i+i] = a1[i];
if (i+1 < n){ // don't put 0 after the last element
a2[i+i+1] = 0;
}
}
}

Resources