Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
using namespace std;
int num(int arr[], int n, int x)
{
    int r = 0;
    for (int i=0; i<n; i++)
    	if (x == arr[i])
          r++;
        return r;
}
 
int main()
{
   
    int m;
    cout<< "Number of elements of array: "<<endl;
    cin>>m;
    int nums[m];
    cout<< "Fill in the array: "<<endl;
    for(int i=0;i<m;i++){
    	cin>>nums[i];
	}
	
    
	int n = sizeof(nums)/sizeof(nums[0]);
    cout << "Array is: ";
    for (int i=0; i < n; i++){
    	cout << nums[i] <<" ";
	}
	cout<<endl;
	int x,elem;
	cout << "Put an element to see how many times is on array: ";
    cin>> x;
    cout <<"\nNumber "<< x <<" is "<< num(nums, n, x)<< " times";
    cout<<"\nPut a new element after the given element: ";
    cin>>elem;
    cout<<"\nNew array is:\n";
    for(int i=0; i<num(nums, n, x); i++){
    	cout <<x<<", "<<elem<<", ";
    }
    cout<<endl;
    return 0;
    }


What I have tried:

 #include <stdio.h>
#include <conio.h>
int num(int arr[], int n, int x)
{
    int r = 0;
    for (int i=0; i<n; i++)
    	if (x == arr[i])
          r++;
        return r;
}
 
int main()
{
   
    int m;
    printf("Number of elements of array:\n ");
    scanf("%d",&m);
    int nums[m];
     printf("Fill in the array:\n ");
    for(int i=0;i<m;i++){
    	scanf("%d", &nums[i]);
	}
	
	int n = sizeof(nums)/sizeof(nums[0]);
    printf("Array is:\n ");
    for (int i=0; i < n; i++){
    	printf("%d", nums[i]); 	
	}
	int x,elem;
	    printf("Put an element to see how many times is on array:: \n ");
	scanf("%d",&x);
    printf("Number" "%d", x ," is " "%d" , num(nums, n, x) ," times");
    printf("Put a new element after the given element:\n ");
    scanf("%d", &elem);
    printf("New array is:\n ");
    for(int i=0; i<num(nums, n, x); i++){
    	printf("%d", x ,"," "%d", elem ,",");
    }
    printf("\n");
    getch();
    return 0;
    }
Posted
Updated 10-May-23 23:21pm
Comments
Rick York 28-Apr-21 11:50am    
No. This is not a code conversion service.
jeron1 28-Apr-21 12:25pm    
I take it that something doesn't work with your attempt? If so, what doesn't work?

You are on the right track!

However, the printf() function only takes one format-string (with the '%' characters in it).

So things like this:

cout << "some text" << x << "some more text" << y;


Have to either (1) have their format strings all merged together; (2) have a "master format string" placed at the beginning; or (3) have to be broken into multiple calls to printf().

Consider this:

int x;
float y;

cout << "some text" << x << "some more text" << y;

printf("some text %d some more text %f", x, y);


In this example, I took the strings, merged in the "%d" and "%f" place-holders for the variables, and produced one big format string. This is how most C programmers would do it, when writing the code from scratch.

Now consider:

cout << "some text" << x << "some more text" << y;

printf("%s %d %s %f", "some text", x, "some more text", y);


In this example, I left everything alone and just put a "master format" string at the front of the arg list. This is how I would do a conversion like you are doing. It's quick, and you don't have to shuffle things around. (In fact, I'd probably write a program to do it for me ;-).

Finally, consider:

cout << "some text" << x << "some more text" << y;

printf("some text" " %d", x); printf("some more text" " %f", y);


In this example, I added the format strings just in front of each variable, because when two strings are next to each other in C, like "foo" "bar" the C compiler will automatically merge them (this is only true for literal strings, not for string variables -- it happens at compile time, not run-time).

Next, I inserted ); printf( after every variable. Then I cleaned up the end.

The result is that each variable has (maybe: a text string, and) a format string in front of it. This is another way that you could mechanically convert to printf.

The thing is, any one of these will work! You just need to pick an approach you like.
 
Share this answer
 
We arent a code conversion service, but you arent the first asking that question so you may find some advice.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900