Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
This is my assignment question and I am trying to solving it but due to lack of time plz help me guys our teacher does not taught recursion function but he give the question regarding it in assignment.
I think we have to write three functions and then give choice to user that from whether option he want to calculate sum ..So I tried this and still working plz help me out..

What I have tried:

I tried this but it have 5 syntax errors and I couldn't figure out them...
C
#include<stdio.h>
int sum1()
{
		int a[10],sum1=0;
		printf("Enter numbers:\n");
		for(int i=1;i<=5;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=5;i++)
	{
		sum1=sum1+a[i];
	}
	return sum1;
}
int sum2()
{
	int a[5],s=0,i=0;
	printf("Enter numbers\n");
	while(i<5)
	{
		scanf("%d",&a[i]);	
	
		s=s+a[i];
			i++;
	}
	return s;
}
int sum3(int arr[], int start, int len)
{
    // Recursion base condition
    if(start >= len)
        return 0;
 
    return (arr[start] + sum3(arr, start + 1, len));
}
int main()
{
		int s;
	s=sum(s);
	printf("the sum of numbers are: %d \n",s);
}
int loop,whileloop,recursion,option;
	printf("Select option from which you want to calculate sum\n");
	scanf("%d",&option);
	
	switch (loop)
	case 1:
	{
	printf("%d",sum1());}
	break;
	switch(whileloop)
	case 2:
	{
		printf("%d",sum2());
	}
	break;
	switch(recursion)
	case 3:
	
	{
		printf("%d",sum3(arr,n,l));
	}
}
Posted
Updated 3-Apr-23 7:34am
v6
Comments
Richard MacCutchan 2-Apr-23 6:31am    
Why do you have two main functions?

Start by looking at the error message - begin with the first one, as a simple error can cause multiple problems to be reported.

Compiling your code with an online service gave this as the first error:
main.c: In function ‘main’:
main.c:40:3: warning: implicit declaration of function ‘sum’; did you mean ‘sum3’? [-Wimplicit-function-declaration]
   40 | s=sum(s);
      |   ^~~
      |   sum3
So, look at the error report: it's "main.c", line 40, column 3, and the problem it found was
Error
implicit declaration of function ‘sum’; did you mean ‘sum3’?
Look at the code - and the error report shows it as:
C
s=sum(s);
a quick look though your code doesn't show a function called sum, just sum1, sum2, and sum3 - so you need to decide which of those you should be calling - and since sum3 requires parameters, it's unlikely to be that one!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 20 minutes for this reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 
v2
Comments
nani jan 2-Apr-23 6:43am    
@OrignalGriff ok I am still working on it but plz just tell me that is my logic is right?
OriginalGriff 2-Apr-23 6:50am    
How would I know? I don't even know which function(s) you are intending to call!
I don't have any access to your assignment question, so I also have no idea exactly what you are meant to achieve - which is pretty fundamental when it comes to "will this work?" type questions ... :D
Try with starting some Learn C tutorial or some video tutorial for your assigment. The reason for your task is to acquire the skills and so you have to learn it. Take the time to understand it because it is in all languages the same. So: "Learn for your life"

some tips: use functions and structs with understandable names and install some IDE like Visual Studio..
 
Share this answer
 
Quote:
plz just tell me that is my logic is right?

What to say about such a source code? Short answer no.
Some syntactical problems have already been mentioned by OriginalGriff, there is a lot more not fitting. The switch-statements don't make sense at all and to read in the values in every function is not useful either. Since each sum should give the same result, you can also call all versions to compare the results. The function with the recursion is still far away from the state it should have.
First of all, the parameters should be the same for all functions.
C
int sum1(int arr[], int n);
int sum2(int arr[], int n);
int sum3(int arr[], int n);

The basic structure for the recursive call could then look like this:
C
int sum3(int arr[], int n)
{
	// Recursion base condition
	if ((n-1) == 0)
		return ...;
	else
		return (... + sum3(arr, n - 1));
}

In the main program you could then test all functions:
C
int s, arr[5];
// ...
for (int option = 0; option < 3; option++) {
   switch (option) {
     case 0:
       s = sum1(arr, 5);
       break;
     case 1:
       s = sum2(arr, 5);
       break;
     case 2:
       s = sum3(arr, 5);
       break;
     }

   printf("sum%d of numbers are: %d \n", option, s);
   }
 
Share this answer
 

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