Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a code written in c. The same code i want to convert to c#. I am unable to get that. Requesting your help.
I do know we have to use Console.Writeline for printf. But what i am not getting is how you will replace &m &n while using the Console.Readline command.
1. I mean how to store the user input values in m and n.
2. Nextly, how to store the cost values in the c[][] array?
and lastly,
3. How to print the total value.

My code in C is as follows:

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,c[100][100],cost,total=0;
clrscr();
printf("Enter number of rows & columns\n");
scanf("%d %d",&m,&n);
printf("Enter cost for each combination\n");
for(i=1;i<=m;i++)
  {
   for(j=1;j<=n;j++)
    {
      scanf("%d", &c[i][j]);
    }
  }
printf("The total cost computation\n");
for(i=1;i<=m;i++)
  {
    for(j=1;j<=n;j++)
      {
        if(c[i][j]>0)
          {
            cost=c[i][j];
            total=total+cost;
          }
       }
   }
printf("the total cost is %d", total);
getch();
}
Posted
Updated 27-Jan-16 0:06am
v6
Comments
Sinisa Hajnal 27-Jan-16 6:01am    
This is very simple function and it stinks of homework. Read rules of questions here. Other then that, as for any two languages: understand what the function does and write it in your target language of choice.
BillWoodruff 27-Jan-16 7:31am    
"stinks of homework" smells like inappropriate hostility.
Member 12247039 27-Jan-16 6:05am    
I am very much new to programming. Well just to know how stuffs work i posted this simple code. I accept its simple but then just wanted to know answers for those three questions so that i could relate the same to my actual code. Its not any kind of homework stuff.

Consider how this code breaks out in the context of a C# Console application:

a. get the number of rows and columns

a.1 create two integer variables,
C#
int rows, columns;
a.2 write a 'while loop that keeps going until the user enters two valid integers for rows/columns
C#
bool arrayParamsIncomplete = true;

while (arrayParamsIncomplete)
{
    // get an integer for rows
    // validate it with Int32.TryParse
    // continue if invalid

   // get an integer for columns
   // validate it with Int32.TryParse
   // continue if invalid

   // exit the loop
   arrayParamsIncomplete = false;
}
b. Use a similar 'while loop to handle the task of reading values in a way that makes sense to you: row by row ? Use String.Split to get the values into an array of 'String, and then use 'Int32.TryParse to validate each entry.

I think you are closer to a solution than you know.
 
Share this answer
 
v3
Use Console.ReadLine where you scanf and Console.WriteLine where you printf. Other than that the code should come over fairly much as it is. If you can't look at that C code and see the c# equivalent then you really need to learn c# first as this is a fairly simple task. We're not here to do your work for you.
 
Share this answer
 
Comments
Member 12247039 27-Jan-16 6:01am    
I have edited my question. I am sorry for not being specific about my problem. I dont want the entire code to be changed. Just want help for those three questions.
F-ES Sitecore 27-Jan-16 6:05am    
Use something like string.Split to split the input string into words (split on " "), then use the various TryParse methods to parse each word into the desired type (eg int.TryParse(mySplitArray[0], out x)). To combile variables into a single string for output use string.Format

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