Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how can i change it to C coding? this is C++ coding... i have do everything but it still stuck

+2 votes
asked Oct 26, 2019 by jihad jannah (140 points)
#include<iostream>

#include<string.h>

using namespace std;

int main(){

int num;

int state;

int a,b,data[num];

string element[2];

cout<<"Enter amount data: ";

cin>>num;

for(a=0;a<num;a++){

cout<<"Enter state: ";

cin>>state;

if(state==3){

element[0]="A";

} else if(state==10){

element[1]="B";

}

}

cout<<"Elements are: ";

for(b=0;b<num;b++){

cout<<" "<<element[b];

}

}

2 Answers

0 votes
answered Oct 27, 2019 by gameforcer (2,990 points)

#include<stdio.h>
#include<string.h>

#define MAX_STR_SIZE 100

int main()
{
    int num;
    int state;
    int a, b;
    char element[2][MAX_STR_SIZE] = {"",""};
    printf("Enter amount data: ");
    
    scanf("%d", &num);
    int  data[num];
    
    for(a=0;a<num;a++)
    {
        printf("Enter state: ");
        scanf("%d", &state);
        
        if(state == 3)
            strcpy(element[0],"A");
        else if(state == 10)
            strcpy(element[1],"B");
    }
    
    printf("Elements are: ");
    
    for(b=0;b<num;b++)
        printf(" %s",element[b]);

    return 0;
}

commented Oct 28, 2019 by jihad jannah (140 points)
edited Oct 28, 2019 by jihad jannah
thank you for respond.. but how about if i put more than 2 elements? example, i want the output to be BAA/ ABA/ AAB/ BBA.. should i add loop? and question no 2 is.. what should i do if the element have more than 2 element.. i mean A B C D E F and so on.. so where should i change and add?
commented Oct 28, 2019 by gameforcer (2,990 points)
1. Simply change:

        char element[2][MAX_STR_SIZE] = {"",""};

to:

        char element[num][MAX_STR_SIZE] = {""};

2. I don't understand the question... You want to have more than just 1 letter stored in your string? Is that it?
commented Oct 29, 2019 by jihad jannah (140 points)
edited Oct 29, 2019 by jihad jannah
yes.. correct. i would like to have input with no limitation letter..
btw, the latest coding that you gave me have below error.

main.c:12:5: error: variable-sized object may not be initialized
     char element[num][MAX_STR_SIZE] = {""};

thanks for your kind help ;)
commented Oct 30, 2019 by gameforcer (2,990 points)
@edit: For your convenience I will post corrected code as another answer here.

1. Ok, to be honest I didn't test it too well so to correct it put

    char element[num][MAX_STR_SIZE];
    for(a=0;a<num;a++)
        element[a][0]='\0';

after

     scanf("%d", &num);

and of course delete the line from earlier which contains

     char element[num][MAX_STR_SIZE] = {""};


2. Replace function

    strcpy(element[0],"A");

with

    strcat(element[0],"A");

Do the same with element[1] and "B".
0 votes
answered Oct 30, 2019 by gameforcer (2,990 points)

 As promised, improved version of the code:

#include<stdio.h>
#include<string.h>

#define MAX_STR_SIZE 100

int main()
{
    int num;
    int state;
    int a, b;
    printf("Enter amount data: ");
    
    scanf("%d", &num);
    int  data[num];
    char element[num][MAX_STR_SIZE];
    
    for(a=0;a<num;a++)
        element[a][0]='\0';
    
    
    for(a=0;a<num;a++)
    {
        printf("Enter state: ");
        scanf("%d", &state);
        
        if(state == 3)
            strcat(element[0],"A");
        else if(state == 10)
            strcat(element[1],"B");
    }
    
    printf("Elements are: ");
    
    for(b=0;b<num;b++)
        printf(" %s",element[b]);

    return 0;
}

If you want your  number of elements to be other than equal to variable num then replace "num" in lines

    char element[num][MAX_STR_SIZE];

    for(a=0;a<num;a++)

with whatever you'd like. 

commented Oct 30, 2019 by jihad jannah (140 points)
thanks a lot for your help.. really appreciated it..

is it possible if the output is return accordingly based on which 'state' i enter first?

 as example,
 if i enter input 3 first and  then 10 the output will be A B
if i enter 10 first  and then 3 , the output will be B A
commented Oct 31, 2019 by gameforcer (2,990 points)
Yes, it is certainly possible and probably relatively easy to do but it's difficult to tell which way of doing it is the best when I don't know the details of the problem that your program should be solving.
Despite that I made you a version which instead of changing the order of displaying it changes order in wchich letters are put in your element array. This way if you ever intend to add more possible states your program will still work just fine.

Here's link to the code:
https://onlinegdb.com/SyBTasD9B
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...