【解決方法】 C++ のコードがあるので、それを C プログラムに変換し、flowgorithm のブロックスキームとして変換する必要があります。 誰か助けてくれませんか?


#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;
    }

私が試したこと:

 #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;
    }

解決策 2

あなたは正しい道を進んでいます!

しかし printf() この関数は、フォーマット文字列 (「%」文字を含む) を 1 つだけ取ります。

したがって、次のようなことになります。

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

(1) フォーマット文字列をすべてマージする必要があります。 (2) 「マスターフォーマット文字列」を先頭に配置します。 または (3) 複数の呼び出しに分割する必要がある printf().

このことを考慮:

int x;
float y;

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

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

この例では、文字列を変数の「%d」および「%f」プレースホルダーに結合して、1 つの大きな書式設定文字列を生成しました。 これは、ほとんどの C プログラマーがコードを最初から作成するときに行う方法です。

ここで次のことを考えてみましょう。

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

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

この例では、すべてをそのままにして、引数リストの先頭に「マスター フォーマット」文字列だけを置きました。 これは、あなたがしているような変換を行う方法です。 すばやく実行できるので、物事を混乱させる必要はありません。 (実際、私はおそらくそれを行うプログラムを書くでしょう ;-)。

最後に、次のことを考慮してください。

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

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

この例では、各変数の直前にフォーマット文字列を追加しました。これは、「foo」「bar」など、C で 2 つの文字列が隣り合っている場合、C コンパイラがそれらを自動的にマージするためです (これはリテラル文字列の場合にのみ当てはまります)。文字列変数の場合ではありません — これは実行時ではなくコンパイル時に発生します)。

次に挿入したのは、 ); printf( すべての変数の後に。 それから端をきれいにしました。

その結果、各変数の前に (おそらく: テキスト文字列と) フォーマット文字列が付きます。 これは、機械的に printf に変換できる別の方法です。

問題は、 これらのいずれかが機能します! 必要なのは、好みのアプローチを選択することだけです。

解決策 1

私たちはコード変換サービスではありませんが、あなたが最初ではありません その質問をする アドバイスが見つかるかもしれません。

コメント

タイトルとURLをコピーしました