use funtion to count this sum:
s=a1*a2+a2*a3+a3*a4+...
I did,but have error: -
#include "stdafx.h"
-
#include<iostream>
-
#include<cmath>
-
using namespace std;
-
void sum(int a[],int n)
-
{
-
int sum;
-
sum=0;
-
for(int i=1;i<n;i++)
-
sum=sum+ a[i]*a[i+1];
-
cout<<"sum="<<sum<<endl;
-
}
-
int main()
-
{
-
int *a[100];
-
int n;
-
cout<<"enter n:"<<endl;
-
scanf("%d",&n);
-
cout<<"enter numbers of array ";
-
for(int i=1;i<n;i++)
-
-
scanf("%d ",&a[i]);
-
sum(a[100],n);
-
getchar();
-
}
-
can you send me your answer into my email : (removed)
thank you very much !
8 1780 Banfa 9,065
Expert Mod 8TB - for(int i=1;i<n;i++)
-
sum=sum+ a[i]*a[i+1];
-
What values does i take in the for loop?
When i = n-1 what is the second line calculating?
What is the valid range for the index i when a has n entries? - int *a[100];
-
int n;
-
...
-
scanf("%d",&n);
-
...
-
scanf("%d ",&a[i]);
-
What is the type of the second parameter passed to scanf the first time it is call?
What is the type of the second parameter passed to scanf the second time it is call?
Which is correct?
You appear to be asking the program to predict what is in the array element
a[i+1]. It could certainly recover the value of a[i-1]. Would a[i]*a[i-1] serve the purpose?
int *a[100];
This is an array of 100 pointers to integer, pointing to nowhere. You use them to store some numbers, it's wrong.
@kiemxai
The '...' leaves the last term of the summation to the imagination.
Which is correct for an array of five elements:
s = a1*a2 + a2*a3 + a3*a4 + a4*a5
or
s = a1*a2 + a2*a3 + a3*a4 + a4*a5 + a5*1
if I do not use the cursor when declared array:
int *a[100];
have the following error:
error C2664: 'sum' : cannot convert parameter 1 from 'int' to 'int []'
so I use the cursor is wrong.can you help me edit that code
Here:
,you are passing a single element of array to the function which expects an array of elements.Pass the whole array instead (sum(a,n);) and it's going to do fine when you remove the 'cursor'.And watch out how arrays are indexed in c/c++.
This might help http://bytes.com/topic/c/insights/77...rrays-revealed @Savage
I do not know much about the cursor. you can for example on my code?
thanks
I used function and declare the array as follows,but still fails:
#include "stdafx.h"
#include<iostream>
#include<cmath>
using namespace std;
void sum(int (*a)[100],unsigned int n)
{
int sum;
sum=0;
for(int i=1;i<n;i++)
sum=sum+ a[i]*a[i+1];
cout<<"sum="<<sum<<endl;
}
int main()
{
int a[100];
int n;
cout<<"enter n:"<<endl;
scanf("%d",&n);
cout<<"enter numbers of array ";
for(int i=1;i<n;i++)
scanf("%d ",&a[i]);
sum(a[100],n);
getchar();
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: truckaxle |
last post by:
I am trying to pass a slice from a larger 2-dimensional array to a
function that will work on a smaller region of the array space. The
code below is a distillation of what I am trying to...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: mtjarrett |
last post by:
i having trouble accessing the values from superglobal arrays. there
are two situations but i'm pretty sure it's the same problem.
here's the deal:
on page1.php i have several check boxes. ...
|
by: Fernando Rodríguez |
last post by:
Hi,
I'm writing code to validate fields in a form before saving to a db. All
the validating functions are in a separate script which is required. All
the validating functions add an error...
|
by: scole954387 |
last post by:
Hi,
I need help with these arrays. I'm using a class (XMLParser.class.php)
to parse the results of a REST request sent to Amazon's server that
returned XML data.
Here's two arrays returned:...
|
by: vfunc |
last post by:
If I have a large array 10,000+ elements then how do I reserve memory
for this ? Currently I get a segmentation fault. Dynamic reservation
is good, but allowing a chunk for the program is an...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: Steve |
last post by:
here's a quirk i can't seem to handle, just hack. since call-time
by-reference is depreciated and i don't want to enable it in the php.ini,
i'm kind of stuck when i want to pass userdata as an...
|
by: Bond |
last post by:
i have written this program of entering 2-d array through
pointers.this shows number of errors.please help me out.
#include<stdio.h>
#include<alloc.h>
void aread(int*,int*,int*);
void...
|
by: Adam Chapman |
last post by:
Hi,
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |