473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array access errors

kiemxai
11
use funtion to count this sum:
s=a1*a2+a2*a3+a3*a4+...
I did,but have error:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<cmath>
  4. using namespace std;
  5. void sum(int a[],int n)
  6. {
  7.     int sum;
  8.     sum=0;
  9.     for(int i=1;i<n;i++)
  10.         sum=sum+ a[i]*a[i+1];
  11.     cout<<"sum="<<sum<<endl;
  12. }
  13. int main()
  14. {
  15. int *a[100];
  16. int n;
  17. cout<<"enter n:"<<endl;
  18. scanf("%d",&n);
  19. cout<<"enter numbers of array ";
  20. for(int i=1;i<n;i++)
  21.  
  22. scanf("%d ",&a[i]);
  23. sum(a[100],n);
  24. getchar();
  25. }
  26.  
can you send me your answer into my email : (removed)
thank you very much !
Apr 8 '09 #1
8 1780
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.     for(int i=1;i<n;i++)
  2.         sum=sum+ a[i]*a[i+1];
  3.  
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?

Expand|Select|Wrap|Line Numbers
  1. int *a[100];
  2. int n;
  3. ...
  4. scanf("%d",&n);
  5. ...
  6. scanf("%d ",&a[i]);
  7.  
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?
Apr 8 '09 #2
whodgson
542 512MB
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?
Apr 8 '09 #3
newb16
687 512MB
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.
Apr 8 '09 #4
donbock
2,426 Expert 2GB
@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
Apr 8 '09 #5
kiemxai
11
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
Apr 8 '09 #6
Savage
1,764 Expert 1GB
Here:
Expand|Select|Wrap|Line Numbers
  1. sum(a[100],n);
,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
Apr 8 '09 #7
kiemxai
11
@Savage

I do not know much about the cursor. you can for example on my code?
thanks
Apr 8 '09 #8
kiemxai
11
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();
}
Apr 8 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
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...
204
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 =...
3
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. ...
3
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...
6
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:...
49
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...
23
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...
12
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...
2
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...
33
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.