Hi,

This is my first post here at DaniWeb. I've been using it as a resource through out my assignments. I am truly eager to learn C++, but I've gotten stuck.

All I am trying to do is average an array of numbers, but I haven't been able to even sum date correctly yet, so I'm taking baby steps.

Here's what I have:

#include <iostream>
#include  <stdlib.h>

int sum(int x[], int dim)
{     int ans=0;
      for (int i=0; i< dim; i++)
          ans += x[i];
      return ans;
}

int main(int argc, char *argv[])
{ 	int x = argc-1;
	int a = argv[x];
	int arr[1000];
    std::cout << "total = " << sum(arr, a) << std::endl;
    return 0;
}

and I'm not even sure if it works because I am receiving this error, which I thought I could fix with "atoi"

error: invalid conversion from ‘char*’ to ‘int’

Any help is greatly appreciated. Thanks

Hi,

This is my first post here at DaniWeb. I've been using it as a resource through out my assignments. I am truly eager to learn C++, but I've gotten stuck.

All I am trying to do is average an array of numbers, but I haven't been able to even sum date correctly yet, so I'm taking baby steps.

Here's what I have:

#include <iostream>
#include  <stdlib.h>

int sum(int x[], int dim)
{     int ans=0;
      for (int i=0; i< dim; i++)
          ans += x[i];
      return ans;
}

int main(int argc, char *argv[])
{ 	int x = argc-1;
	int a = argv[x];
	int arr[1000];
    std::cout << "total = " << sum(arr, a) << std::endl;
    return 0;
}

and I'm not even sure if it works because I am receiving this error, which I thought I could fix with "atoi"

error: invalid conversion from ‘char*’ to ‘int’

Any help is greatly appreciated. Thanks

Well, since you didn't bother to tell us where the error is we can only guess. It is always helpful to give us all the information you can. Don't leave things out. You never know what would be important.

My guess is you have a problem with the line int a = argv[x]; Questions to answer:
1) Is argv[x] an integer?
2) What type of variable is it?
3) Now, what does the error say?
4) Got it now?

Hope so...

1) Is argv[x] an integer? I'm assuming it's an integer, yes. Why wouldn't it be? It's the number of values in the array minus one, correct?
2) What type of variable is it? Int?
3) Now, what does the error say?

sum1.c: In function ‘int main(int, char**)’:
sum1.c:18: error: invalid conversion from ‘char*’ to ‘int’

I apologize for my lack of knowledge, I'm aware this makes helping me a bit harder.

1) Is argv[x] an integer? I'm assuming it's an integer, yes. Why wouldn't it be? It's the number of values in the array minus one, correct?

you can't assume. You have to know. See where assuming got you? :icon_wink: int main(int argc, char *argv[]) Look carefully. What is argv[x]?

you can't assume. You have to know. See where assuming got you? :icon_wink: int main(int argc, char *argv[]) Look carefully. What is argv[x]?

Ohh. It's a char. Hahah. Hmm. I changed that and got

sum1.c: In function ‘int main(int, char**)’:
sum1.c:18: error: invalid conversion from ‘char*’ to ‘char’

Thanks for pointing that out.

well are you trying to sum up the integers from initial to final and need average of it or what i didnt get exactly..

well are you trying to sum up the integers from initial to final and need average of it or what i didnt get exactly..

I'm trying to sum all the number inputted into the array.

For example I type

a.out 8 3 5

The sum would be 16.

That's all I'm looking to do, but it could be any amount of numbers, not just 3. If I can do this, then I can surely average them by the "argc"

So where are you now? Remember, we can't see your screen. And we can't read your mind...

So where are you now? Remember, we can't see your screen. And we can't read your mind...

Basically the same place, but instead of getting the error

sum1.c:18: error: invalid conversion from ‘char*’ to ‘int’

I'm getting

sum1.c:18: error: invalid conversion from ‘char*’ to ‘char’

This is an error I haven't not be able to fix so I am unable to test out the program.

So I've gotten my code down finally, and it works. The only problem I am having is I can't get the output to display decimals. I've placed the "float" data type almost everywhere, to no avail.

Here's my code.

#include <iostream>
#include  <stdlib.h>

float sum(float x[], float dim)
{     float ans=0;
      for (int i=0; i< dim; i++)
          ans += x[i];
      return ans;
}

int echo(int argc, char *argv[])
{   int e = 1; 
    while (e < argc )
    {  std::cout << argv[e++]  
                 << " ";     
    }
    std::cout << std::endl;
    return 0;
}

int main(int argc, char *argv[])
{ 	float arr[argc-1];
	for (int i=1; i< argc; i++) 
    {     arr[i-1] = atoi(argv[i]);
	}
	int e = 1;
	float d = argc-1;
	float av = sum(arr, (argc-1))/d;
	std::cout << "You've inputted "; 
    	while (e < argc )
    	{  std::cout << argv[e++]  
             	    << " ";
   	 	}
    std::cout << "The total is = " << sum(arr, (argc-1)) 
    	<< " The average is = " << av << std::endl;
    return 0;
}

Once you get the data from argc and argv, stop using the values. All your data is in your variables now. Just use the variables.

Member Avatar for MonsieurPointer
arr[i-1] = atoi(argv[i]);

You have defined arr as float, but atoi converts char* to int. Take a look at atof.

float d = argc-1;

argc is an int. Why did you choose fload for d??

Before you ask about why your code doesn't print out decimals, you first need to maintain consistent data types. Mixing them up can only lead to casting / conversion problems later down the road...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.