how to change this few code into C programming?(from C++

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int T;
scanf("%d", &T);
while (T--) {
int M, N, num[30000];
scanf("%d%d", &M, &N);
for (int i = 0; i < M; i++) {
scanf("%d", &num[i]);
}
vector<int> box;
int done = 0, need = 0;
while (N--) {
int index;
scanf("%d", &index);
while (box.size() < index) {
vector<int>::iterator it = lower_bound(box.begin(), box.end(), num[done]);
box.insert(it, num[done++]);
}
printf("%d\n", box[need++]);
}
if (T) {
puts("");
}
}
return 0;
}
use <> code tags!
ok, this is your code with indentation.
but what is it supposed to do ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int M, N, num[30000];
		scanf("%d%d", &M, &N);
		for (int i = 0; i < M; i++) {
			scanf("%d", &num[i]);
		}
		vector<int> box;
		int done = 0, need = 0;
		while (N--) {
			int index;
			scanf("%d", &index);
			while (box.size() < index) {
				vector<int>::iterator it = lower_bound(box.begin(), box.end(), num[done]);
				box.insert(it, num[done++]);
			}
			printf("%d\n", box[need++]);
		}
		if (T) {
			puts("");
		}
	}
	
return 0;
It is the code from http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=442
but the code is C++,i dont know how to change to C programming
thanks for helping!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>
#include <vector> // c++
#include <algorithm> // never used
using namespace std; // c++

int main() 
{
    int T; 
    scanf("%d", &T); 
    while (T--) 
    {
        int M, N, num[30000];
        scanf("%d%d", &M, &N);
        for (int i = 0; i < M; i++)
        {
            scanf("%d", &num[i]);
        }
        vector<int> box; // C++
        int done = 0, need = 0;
        while (N--) 
        {
            int index;
            scanf("%d", &index);
	    while (box.size() < index) // C++
            {
                vector<int>::iterator it = lower_bound(box.begin(), box.end(), num[done]); // C++
                box.insert(it, num[done++]); // C++
            }
            printf("%d\n", box[need++]);
        }
        if (T) 
        {
	    puts("");
        }
    }
	
    return 0;
}


Here you go, box is the only element that's not portable to C so all lines containing box have to be replaced somehow ;)
or you can implement vector<int> and vector<int>::iterator with structs and without templates in C :b
Last edited on
@Gamer2015 & @anup30

Both of you still have vectors and iterators in the code he needed in the language C. Im wondering, are vectors and iterators part of C. Werent they introduced in c++?
I didn't change anything in his code.
I just marked the lines that need C++.

wrote that at the end of my post ^^
Last edited on
Yes you did. Thanks brain...
Last edited on
TarikNeaj wrote:
Both of you still have vectors and iterators in the code he needed in the language C

did you read carefully what i wrote?
ok, this is your code with indentation.
but what is it supposed to do ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h> // qsort()

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main() {

	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	int T;
	int ADD[30000], aCount, GET, gCount;
	scanf("%d", &T);

	while (T--) {

		scanf("%d%d", &aCount, &gCount);
		for(int i=0; i<aCount; i++){
			scanf("%d", &ADD[i]);
		}
		int got = 0;
		for(int i=0; i<gCount; i++){
			scanf("%d", &GET);
			qsort(ADD, GET, sizeof(int), compare);
			printf("%d\n", ADD[got++]);
		}
		printf("\n");
	}

return 0;
}
1
2
freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);

i think these 2 sentence of code is not allowed for C program
i think these 2 sentence of code is not allowed for C program
ORLY?
http://en.cppreference.com/w/c/io/freopen
i mean "Standard output is empty" from ideone.com
how to adjust the code so that I can enter the input rather than create a file??
sorry i am a beginner,i need a template
Last edited on
sirrunrun wrote:
i think these 2 sentence of code is not allowed for C program

i thought you'll at least try the code in your C compiler!
Of course it would be empty as you explicitely redirected everything going to the standard output to a file. And ideone cannot work with files. Actually I ever seen only two online compilers which can read from files and only one which can write to some explicitely allowed files.

Do not use online compilers if you need more than basic proof-of-concept things.
thanks I know how to use input/out file now~
but I do not know how to change the following C++ code to C ...can anyone help me?thanks
this is the code from the question of poj1760~but i dont know how to changing to C program
http://poj.org/problem?id=1760

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 #include <string>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;

#define MAXNODE 20000
#define MAXLEN 85
#define MAXWORD 45

map<string, int> m[MAXNODE];

void preOder_traversal(int midx, int indentation)
{
	for (map<string, int>::iterator i=m[midx].begin(); i!=m[midx].end(); ++i)
	{
		for (int i=0; i<indentation; ++i)	printf(" ");
		printf("%s\n", (i->first).c_str());

		preOder_traversal(i->second, indentation+1);
	}
}

int main()
{
	//freopen("D:\\in.txt", "r", stdin);
	//freopen("D:\\out.txt", "w", stdout);

	int n, i, j, midx, midx_counter=0;
	char str[MAXLEN];
	int word_init[MAXWORD];
	string current_str;

	scanf("%d", &n);

	while (n--)
	{
		scanf("%s", str);

		//
		word_init[0]=0;
		j=1;
		for (i=0; str[i]; ++i)	
		{
			if(str[i]=='\\')
			{
				str[i]=0;
				word_init[j++]=i+1;	
			}
		}

		midx=0;	
		for (i=0; i<j; ++i)	
		{
			current_str=str+word_init[i];

			if(!m[midx].count(current_str))	
			{
				m[midx][current_str]=(++midx_counter);
			}

			midx=m[midx][current_str];
		}
	}

	preOder_traversal(0, 0);

	return 0;
}
This is discussed in another thread: http://www.cplusplus.com/forum/beginner/159768/

And I will repeat again: you need several thousands lines of code to port map to C.
Additionally this code is terrible and suboptimal. It should be forgotten, not studied and ported.
@sirrunrun, you will need very good linked list understanding, to solve last problem in C.

besides linked lists, another idea comes to mind is c-string sorting(lexicographic, in between backspaces "\" ). but it can get very complicated too. because you have to sort according to various inner layers.

... so its better to try easier problems!
Topic archived. No new replies allowed.