Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii every one .. I have a code and now i have to use index or parameters in this code... can anybody help me.. my code is... first read a file and divide 10 elemets in first array and next 10 elements in second aaray.. After that change character into ascii value and sum of the ascii value... please help me ...

C++
#include <stdio.h>
#include <string.h>

int funcA(char *ch);
void main()
{
FILE *fp;
char ch[100];
char ch2[10];
char c;

int i=0;
int a=0;
int num=0;//

//int i=0;
int j=0;
int k=10;
int sum=0;

fp=fopen("a.txt","rt");

while(feof(fp)==0)
{
    c=fgetc(fp);
    /*
   if(c=='\0')
    {
    //ch[i]='\0';
	break
	}
	*/
	//else
	//{
	ch[i]=c;// 
	//}
	printf("%c",ch[i]);
	i++;
	num++;

}
printf("\n%d\n",num-1);
while(1){
	printf("\n");
	for(i=j,a=0;i<k,a><10;i++,a++){>
		if(i>=num)
		{
			//funcA(ch2);
			printf(":%d\n",funcA(ch2));
			exit(0);
		}
		else
		{
			ch2[a]=ch[i];
			printf("%c",ch[i]);
		}
	}
	printf(":%d\n",funcA(ch2));
	//printf("%d",su);
	j+=10;
	k+=10;
}
fclose(fp);
return 0;
}
int funcA(char *ch){
	int i=0;
	int sum=0;
	printf("(");
	for(i=0;i<10;i++)
	{if(ch[i]<=0)
	{ break;}
	sum+=ch[i];
	printf("%d",ch[i]);

	}

printf(")");
//printf(": %d\n",sum);
return sum;
}
Posted
Updated 15-Apr-15 23:06pm
v2
Comments
OriginalGriff 16-Apr-15 5:26am    
I'm not looking at that code.
Do yourself a favour: indent it properly (so we can see what applies to what) and change the variable names so they reflect what they are used for. WHen everything is called "i", "j", and "k" it gets very difficult to work out what the heck is going on - particularly when your function names are as uninformative. If you aren't going to comment your code, then it needs to be self documenting.
And that isn't either!
KarstenK 17-Apr-15 7:20am    
You should debug the code for yourself. Make output for all unclear values. Use some well designed input data.

Most: use the Debugger

1 solution

You really only need one char array, say char C[10] and an int n for counting.
Then one function (or memset) to make all zero, before reading up to next 10 chars.
The fread function should read in max 10 chars and set n to number of chars read.

For ASCII sum, the function should look like:

C++
int sumchars(char* arr, int n){}


Then use for loop like you have to add up the chars.

Another way is to make your char array with 11 elements and always set last element to zero.

Then, your method can sum chars while *arr is not equal to zero.
 
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