good time people
I have a code like the one below:

``````#include <vector>
#include <stdio.h>
#include "mymath.h"
using namespace std;
int main(){
int n;
char a='y';
do{
cout<<"\n----------------------------------------\n";
cout<<"Enter a number for prime factorization : ";
cin>>n;
cout<<"\n----------------------------------------\n";
int *result=pfact(n);
for(int i;result[i][0]!=0;++i){
if(result[i+1][0]!=0) cout<<result[i][0]<<"^"<<result[i][1]<<"*";
else cout<<result[i][0]<<"^"<<result[i][1]<<"=";}
cout<<n;
cout<<"\n----------------------------------------\n";
cout<<"Do you want to continue (y/n) ?  ";
cin>>a;}while(a=='y');}``````

with having mymath.h like the one below:

``````#ifndef MYMATH_H_INCLUDED
#define MYMATH_H_INCLUDED

#endif // MYMATH_H_INCLUDED
#include <stdio.h>
#include <vector.h>
int *pfind(int num){
int index=0,*prime;
prime=new int[num];
for(int i=1;i<=num;++i){
int flag=0;
for(int j=2;j<i;++j){
if(i%j==0){
++flag;}}
if(flag==0 && i>1){
prime[index]=i;
++index;}}
prime[index]=0;
return prime;}
int *pfact(int num){
int *prime=pfind(num/2);
int re[num][2];
int index=0;
for(int i=0;prime[i]!=0;++i){
for(int j=1;num%prime[i]==0;++j){
re[index][0]=prime[i];
re[index][1]=j;}
++index;}
re[index][0]=0;
return *re;}``````

when I run the program,I'll get six errors like:

error : invalid types 'int[int]' for array subscript

for every use of array result.
thanks

6
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by iamthwee

Whatever you do, don't tell us what line numbers these errors happened on.

Since you are just finding the prime factors of a number,how about something like this::

``````//fragment code where num is your real number
if(num>0)
{

for(int i=1;i<=num;++i)
{

if(i==1)
{
cout<<i<<" ";//Can be done without it
continue;//Don't dare get into a while loop with 1
}

while(num%i==0)
{
cout<<i<<" ";//Successively divide  any number
//as every composite number can
// be expressed as multiple of  prime  numbers

num/=i;

}

}

}``````

You can't run this program; moreover, you can't compile it:

``````int *result=pfact(n); // You declare a pointer to int
for (int i; result[i][0] !=0; ++i) {
// You are trying to subscript result
if(result[i+1][0]!=0) ...
// result[i] is int and now you are trying
// to index this int ???!!!``````

Never place function definitions in .h files (only function prototypes).

I have the same problem and get the same error.
Any ideea how I can initialise the matrix ?, in this case to make the result[0]=0;
Sorry if the question is too banal.

You can't run this program; moreover, you can't compile it:

``````int *result=pfact(n); // You declare a pointer to int
for (int i; result[i][0] !=0; ++i) {
// You are trying to subscript result
if(result[i+1][0]!=0) ...
// result[i] is int and now you are trying
// to index this int ???!!!``````

Never place function definitions in .h files (only function prototypes).