You are not logged in. Please login at www.codechef.com to post your questions!

×

getting wrong ans

#include<stdio.h>
#include<conio.h>

int tri(int,int,int); int a[100][100]; int s1,s2,val;

main()
{
    int n,m,i,j,sum,k;
    scanf("%d",&m);
    for(k=1;k<=m;k++)
    {
        scanf("%d",&n);

        for(i=0;i<n;i++)
        {
            for(j=0;j<=i;j++)
            scanf("%d",&a[i][j]);
            printf("\n");

        }
sum=tri(0,0,n);
printf("%d\n",sum);
    }

getch();

}

int tri(int i,int j,int n)
{
if(i>=(n-1))
{
val= a[i][j];}
else
{
    s1=a[i][j]+tri(i+1,j,n);
    s2=a[i][j]+tri(i+1,j+1,n);
if(s1>s2)
val=s1;
  val=s2;
  } 
return val;

}

asked 08 Aug '14, 18:01

sarique's gravatar image

1★sarique
-1119
accept rate: 0%

edited 08 Aug '14, 18:54

kcahdog's gravatar image

3★kcahdog
10.0k2854129


Since you have not specified which question this code is for, let's assume it's for Life,the Universe and Everything. Now in this question you have to print all numbers until you encounter the number 42 but you are just doing some random operations on a 2D array which is difficult to understand !!!!!!!!! That's why WA.

Ok on a serious note Please give the question link and (hopefully) a more commented code and we will be glad to help. I tried finding the question from your profile but you have not solved anything for 2 months so it was no help!

link

answered 08 Aug '14, 18:53

kcahdog's gravatar image

3★kcahdog
10.0k2854129
accept rate: 14%

C programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. and also to make use of array you need to put a for loop or any loop and to give its body you need to put braces {} in the first image the braces is missing

link

answered 09 Aug '14, 21:38

amardeep_singh's gravatar image

2★amardeep_singh
-3
accept rate: 0%

he above statement will take 10th element from the array and assign the value to salary variable. Following is an example which will use all the above mentioned three concepts viz. declaration, assignment and accessing arrays:

include <stdio.h>

int main () { int n[ 10 ]; / n is an array of 10 integers / int i,j;

/ initialize elements of array n to 0 /
for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; / set element at location i to i + 100 / }

/ output each array element's value / for (j = 0; j < 10; j++ ) { printf("Element[%d] = %d\n", j, n[j] ); }

return 0; } When the above code is compiled and executed, it produces the following result:

Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109

.. to pass a value in a function ... you need to use some formal variables which are in main() function for eg main() {

func(int a, int b) } int func (int x,int y) {

so here when the calculation in your self defined function gets over the value of x which is for function only which is considered as 'a' variable in main() gets transferred to a

means......... a=x x value goes to a after calculation ........ b=y; same thing .... now check your program and read a C book again

link

answered 09 Aug '14, 21:45

amardeep_singh's gravatar image

2★amardeep_singh
-3
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×849
×334
×135
×30
×24
×9

question asked: 08 Aug '14, 18:01

question was seen: 776 times

last updated: 09 Aug '14, 21:45