Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:44:44+00:00 2026-05-26T21:44:44+00:00

#include<stdio.h> int max(int a,int b) { if(a>b) return a; else return b; } void

  • 0
#include<stdio.h>

int max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}

void knapsack(int m,int n,int w[],int p[])
{
    int v[10][10],x[10],i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
        {
            if(j==0||i==0)
                v[i][j]=0;
            if(j-w[i]<0)
                v[i][j]=v[i-1][j];
            else
                v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
        }
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
            printf("%d\t",v[i][j]);
        printf("\n");
    }
    printf("THE OPTIMAL SOLUTION IS:%d",v[n][m]);
    for(i=1;i<=n;i++)
        x[i]=0;
    i=n;
    j=m;
    while(i>0 && j>0)
    {
        if(v[i][j]!=v[i-1][j])
        {
            x[i]=1;
            j=j-w[i];
        }
        i--;
    }
    printf("THE OPTIMAL SET OF WEIGHTS IS:");
    for(i=1;i<=n;i++)
        if(x[i]==1)
            printf("%d\t",i);
    printf("\n");
}

int main()
{
    int w[10],p[10],i,m,n;
    printf("ENTER THE NUMBER OF ITEMS:");
    scanf("%d",&n);
    printf("ENTER THE WEIGHTS OF THE ITEMS:");
    for(i=1;i<=n;i++)
        scanf("%d",&w[i]);
    printf("ENTER THE PROFITS OF THE ITEMS:");
    for(i=1;i<=n;i++)
        scanf("%d",&p[i]);
    printf("ENTER THE CAPACITY OF KNAPSACK:");
    scanf("%d",&m);
    knapsack(m,n,w,p);
    return 0;
}

SAMPLE OUTPUT:

chaitanya@chaitanya-laptop:~/Desktop/My prog$ ./a.out

ENTER THE NUMBER OF ITEMS:5

ENTER THE WEIGHTS OF THE ITEMS:3
2
1
2
3

ENTER THE PROFITS OF THE ITEMS:2
3
2
3
2

ENTER THE CAPACITY OF KNAPSACK: 8

0   -72 -1080992920 -72 0   1   -1080993280 0   13403040    
0   -72 -1080992920 2   0   1   -70 2   13403040    
0   -72 3   2   0   5   3   4   13403040    
0   2   3   5   4   5   7   5   13403040    
0   2   3   5   6   8   7   8   13403040    
0   2   3   5   6   8   7   8   13403040    

THE OPTIMAL SOLUTION IS:13403040

THE OPTIMAL SET OF WEIGHTS IS:

Note: The same program produces a legitimate output for the same input when compiled in the “Turbo C” compiler.

So that leads me to believe that i am not adhering to C standards. Is that so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:44:44+00:00Added an answer on May 26, 2026 at 9:44 pm

    When you initialize w you are using 1-based indexing:

    for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
    

    But when you access it, you are using 0-based indexing.

    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
        {
            if(j==0||i==0)
                v[i][j]=0;
            if(j-w[i]<0)   // This line accesses w[0] when i is 0. Missing an else?
                v[i][j]=v[i-1][j];
            else
                v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
        }
    }
    

    In C arrays use 0-based indexing. Change your code to use 0-based indexing consistently.

    Also, you should check the return value of scanf otherwise invalid input will give strange results instead of an error.

    for (i=0; i < n; i++) {
        if (scanf("%d", &w[i]) != 1) {
            return EXIT_FAILURE; // Handle the error appropriately.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> void function(int); int main() { int x; printf(Enter x:); scanf(%d, &x); function(x); return
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> void foo(int **arr) { arr[1][1]++; } main() { int arr[20][20]; printf(%d\n,arr[1][1]); foo((int**)arr); printf(%d\n,arr[1][1]);
#include<stdio.h> int main() { int a,b; a=a+b; printf(%d,a); return 0; } what should be
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int
#include <stdio.h> int main() { char read = ' '; while ((read = getchar())
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
Consider this C code: #include stdio.h int main(void) { int count = 5; unsigned
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.