hailholyghost

May 28th, 2012, 02:48 AM

Hello, I have the following code in C:

#include <stdio.h>

#include <math.h>

int main (int argc, char *argv[])

{

long i = atoi(argv[1]);

long high = i/2;

long factors[high];

long primes[high];

long exp[high];

long x;

long index = 0;

long primeindex = 0;

long y;

for (x = 2; x < high; x++) {

if (i % x == 0) {

factors[index] = x;

index++;

}

short tmp;

for (y = 2; y < x-1; y++) {

if (x % y == 0) {

tmp++;

break;

}

}

if (tmp == 0) {

int exp = 1;

int try = pow(x,exp);

while (i % try == 0) {

exp++;

try = pow(x,exp);

}

if (exp > 1) {

primes[primeindex] = x;

long z = exp-1;

exp[primeindex] = z;

primeindex++;

}

}

}

return 0;

}

The compiler error is on line 37:

"subscripted value is neither array nor pointer". The purpose of that line is to mimic Perl's "push" function and have two identically indexed arrays like Perl's associative array. Obviously I was not successful. How can I write to the arrays?

The purpose of this exercise is for me to learn C, so I'm translating a Perl code I wrote (which works perfectly, I would just like to make it faster so it could handle larger numbers):

#!/usr/bin/perl

use strict;

use warnings;

if ((@ARGV==0) || (int($ARGV[0]) != $ARGV[0])) {

print "You did not enter an integer. The proper syntax is \"perl factors.pl integer\"\n";

} else {

my $upper = $ARGV[0]/2;

my @factors;

my %primefactors;

#find primes below $ARGV[0]

foreach my $x (2..$upper) {

if ($ARGV[0] % $x == 0) {

push(@factors,$x);

}

my $tmp = 0;

foreach my $y (2..$x-1) {

if ($x % $y == 0) {

$tmp++;

last;

}

}

if ($tmp == 0) {

my $exponent = 1;

while (($ARGV[0] % (($x)**$exponent)) == 0) {

$exponent++;

}

if ($exponent > 1) {

$primefactors{$x} = $exponent-1;

}

}

}

#write prime factorization

if (@factors == 0) {

print "$ARGV[0] is prime.

The prime factorization of $ARGV[0] is $ARGV[0]^1.\n";

} else {

my $answer;

foreach (sort{$a <=> $b} keys(%primefactors)) {

$answer .= "($_^$primefactors{$_})";

}

print "The factors of $ARGV[0] are: @factors

The Prime Factorization of $ARGV[0] is $answer\n";

}

}

#include <stdio.h>

#include <math.h>

int main (int argc, char *argv[])

{

long i = atoi(argv[1]);

long high = i/2;

long factors[high];

long primes[high];

long exp[high];

long x;

long index = 0;

long primeindex = 0;

long y;

for (x = 2; x < high; x++) {

if (i % x == 0) {

factors[index] = x;

index++;

}

short tmp;

for (y = 2; y < x-1; y++) {

if (x % y == 0) {

tmp++;

break;

}

}

if (tmp == 0) {

int exp = 1;

int try = pow(x,exp);

while (i % try == 0) {

exp++;

try = pow(x,exp);

}

if (exp > 1) {

primes[primeindex] = x;

long z = exp-1;

exp[primeindex] = z;

primeindex++;

}

}

}

return 0;

}

The compiler error is on line 37:

"subscripted value is neither array nor pointer". The purpose of that line is to mimic Perl's "push" function and have two identically indexed arrays like Perl's associative array. Obviously I was not successful. How can I write to the arrays?

The purpose of this exercise is for me to learn C, so I'm translating a Perl code I wrote (which works perfectly, I would just like to make it faster so it could handle larger numbers):

#!/usr/bin/perl

use strict;

use warnings;

if ((@ARGV==0) || (int($ARGV[0]) != $ARGV[0])) {

print "You did not enter an integer. The proper syntax is \"perl factors.pl integer\"\n";

} else {

my $upper = $ARGV[0]/2;

my @factors;

my %primefactors;

#find primes below $ARGV[0]

foreach my $x (2..$upper) {

if ($ARGV[0] % $x == 0) {

push(@factors,$x);

}

my $tmp = 0;

foreach my $y (2..$x-1) {

if ($x % $y == 0) {

$tmp++;

last;

}

}

if ($tmp == 0) {

my $exponent = 1;

while (($ARGV[0] % (($x)**$exponent)) == 0) {

$exponent++;

}

if ($exponent > 1) {

$primefactors{$x} = $exponent-1;

}

}

}

#write prime factorization

if (@factors == 0) {

print "$ARGV[0] is prime.

The prime factorization of $ARGV[0] is $ARGV[0]^1.\n";

} else {

my $answer;

foreach (sort{$a <=> $b} keys(%primefactors)) {

$answer .= "($_^$primefactors{$_})";

}

print "The factors of $ARGV[0] are: @factors

The Prime Factorization of $ARGV[0] is $answer\n";

}

}