Thursday, 23 August 2012

averaging using pointers


#include<iostream>
using std::cin;
using std::cout;
#include<conio.h>
void main()
{
int *p,n,a[100],i,j;
float sum=0,avg;
cout<<"enter the no of elements: ";
cin>>n;
cout<<"\nenter the elements-\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(p=&a[0];p<=&a[n-1];p++)
{
sum=sum+*p;
}
avg=sum/n;
cout<<"\nthe average= "<<avg;
getch();
}




1 comment:







  1. include io.h

    cr EQU 0ah
    lf EQU 0dh

    data segment
    n1 db cr , lf, "enter number",0
    n2 db cr,lf, "number is even",0
    n3 db cr,lf ,"number is odd",0
    n4 db cr,lf,"enter 2",0
    n5 db cr,lf,"Done!",0

    x dw ?
    y dw ?
    num dw 40 dup(?)
    data ends

    code segment
    assume cs:code , ds:data
    start:
    mov ax,data
    mov ds,ax

    output n1
    inputs num,40
    atoi num
    mov x,ax

    output n4
    inputs num,40
    atoi num
    mov y,ax

    mov bl,y
    mov al,x
    div bl
    cmp ah,00h
    JE lable
    output n3
    jmp quit
    lable:
    output n2
    jmp quit

    quit:mov ax, 4c00h
    int 21h
    code ends
    end startC:\masm\masm>masm qwe.asm
    Microsoft (R) Macro Assembler Version 5.10
    Copyright (C) Microsoft Corp 1981, 1988. All rights reserved.

    Object filename [qwe.OBJ]:
    Source listing [NUL.LST]:
    Cross-reference [NUL.CRF]:
    qwe.asm(34): warning A4031: Operand types must match
    qwe.asm(35): warning A4031: Operand types must match

    48684 + 401471 Bytes symbol space free

    2 Warning Errors
    0 Severe Errors

    C:\masm\masm>link qwe+io

    Microsoft (R) 8086 Object Linker Version 3.04
    Copyright (C) Microsoft Corp 1983, 1984, 1985. All rights reserved.

    Run File [QWE.EXE]:
    List File [NUL.MAP]:
    Libraries [.LIB]:

    C:\masm\masm>qwe

    enter number6

    enter 22

    number is even
    C:\masm\masm>qwe

    enter number7

    enter 22

    number is odd
    C:\masm\masm>



    ReplyDelete